ggml-quants.c 209 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230
  1. #define GGML_COMMON_IMPL_C
  2. #include "ggml-common.h"
  3. #include "ggml-quants.h"
  4. #include "ggml-impl.h"
  5. #include "ggml-cpu/ggml-cpu-impl.h"
  6. #include "ggml-cpu.h"
  7. #include <math.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <float.h>
  11. #include <stdlib.h> // for qsort
  12. #include <stdio.h> // for GGML_ASSERT
  13. #define GROUP_MAX_EPS 1e-15f
  14. #define GROUP_MAX_EPS_IQ3_XXS 1e-8f
  15. #define GROUP_MAX_EPS_IQ2_S 1e-8f
  16. #define GROUP_MAX_EPS_IQ1_M 1e-7f
  17. #define GROUP_MAX_EPS_IQ1_S 1e-12f
  18. #define UNUSED GGML_UNUSED
  19. // reference implementation for deterministic creation of model files
  20. void quantize_row_q4_0_ref(const float * GGML_RESTRICT x, block_q4_0 * GGML_RESTRICT y, int64_t k) {
  21. static const int qk = QK4_0;
  22. assert(k % qk == 0);
  23. const int nb = k / qk;
  24. for (int i = 0; i < nb; i++) {
  25. float amax = 0.0f; // absolute max
  26. float max = 0.0f;
  27. for (int j = 0; j < qk; j++) {
  28. const float v = x[i*qk + j];
  29. if (amax < fabsf(v)) {
  30. amax = fabsf(v);
  31. max = v;
  32. }
  33. }
  34. const float d = max / -8;
  35. const float id = d ? 1.0f/d : 0.0f;
  36. y[i].d = GGML_FP32_TO_FP16(d);
  37. for (int j = 0; j < qk/2; ++j) {
  38. const float x0 = x[i*qk + 0 + j]*id;
  39. const float x1 = x[i*qk + qk/2 + j]*id;
  40. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  41. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  42. y[i].qs[j] = xi0;
  43. y[i].qs[j] |= xi1 << 4;
  44. }
  45. }
  46. }
  47. void quantize_row_q4_1_ref(const float * GGML_RESTRICT x, block_q4_1 * GGML_RESTRICT y, int64_t k) {
  48. const int qk = QK4_1;
  49. assert(k % qk == 0);
  50. const int nb = k / qk;
  51. for (int i = 0; i < nb; i++) {
  52. float min = FLT_MAX;
  53. float max = -FLT_MAX;
  54. for (int j = 0; j < qk; j++) {
  55. const float v = x[i*qk + j];
  56. if (v < min) min = v;
  57. if (v > max) max = v;
  58. }
  59. const float d = (max - min) / ((1 << 4) - 1);
  60. const float id = d ? 1.0f/d : 0.0f;
  61. y[i].d = GGML_FP32_TO_FP16(d);
  62. y[i].m = GGML_FP32_TO_FP16(min);
  63. for (int j = 0; j < qk/2; ++j) {
  64. const float x0 = (x[i*qk + 0 + j] - min)*id;
  65. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  66. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  67. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  68. y[i].qs[j] = xi0;
  69. y[i].qs[j] |= xi1 << 4;
  70. }
  71. }
  72. }
  73. void quantize_row_q5_0_ref(const float * GGML_RESTRICT x, block_q5_0 * GGML_RESTRICT y, int64_t k) {
  74. static const int qk = QK5_0;
  75. assert(k % qk == 0);
  76. const int nb = k / qk;
  77. for (int i = 0; i < nb; i++) {
  78. float amax = 0.0f; // absolute max
  79. float max = 0.0f;
  80. for (int j = 0; j < qk; j++) {
  81. const float v = x[i*qk + j];
  82. if (amax < fabsf(v)) {
  83. amax = fabsf(v);
  84. max = v;
  85. }
  86. }
  87. const float d = max / -16;
  88. const float id = d ? 1.0f/d : 0.0f;
  89. y[i].d = GGML_FP32_TO_FP16(d);
  90. uint32_t qh = 0;
  91. for (int j = 0; j < qk/2; ++j) {
  92. const float x0 = x[i*qk + 0 + j]*id;
  93. const float x1 = x[i*qk + qk/2 + j]*id;
  94. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  95. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  96. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  97. // get the 5-th bit and store it in qh at the right position
  98. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  99. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  100. }
  101. memcpy(&y[i].qh, &qh, sizeof(qh));
  102. }
  103. }
  104. void quantize_row_q5_1_ref(const float * GGML_RESTRICT x, block_q5_1 * GGML_RESTRICT y, int64_t k) {
  105. const int qk = QK5_1;
  106. assert(k % qk == 0);
  107. const int nb = k / qk;
  108. for (int i = 0; i < nb; i++) {
  109. float min = FLT_MAX;
  110. float max = -FLT_MAX;
  111. for (int j = 0; j < qk; j++) {
  112. const float v = x[i*qk + j];
  113. if (v < min) min = v;
  114. if (v > max) max = v;
  115. }
  116. const float d = (max - min) / ((1 << 5) - 1);
  117. const float id = d ? 1.0f/d : 0.0f;
  118. y[i].d = GGML_FP32_TO_FP16(d);
  119. y[i].m = GGML_FP32_TO_FP16(min);
  120. uint32_t qh = 0;
  121. for (int j = 0; j < qk/2; ++j) {
  122. const float x0 = (x[i*qk + 0 + j] - min)*id;
  123. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  124. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  125. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  126. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  127. // get the 5-th bit and store it in qh at the right position
  128. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  129. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  130. }
  131. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  132. }
  133. }
  134. // reference implementation for deterministic creation of model files
  135. void quantize_row_q8_0_ref(const float * GGML_RESTRICT x, block_q8_0 * GGML_RESTRICT y, int64_t k) {
  136. assert(k % QK8_0 == 0);
  137. const int nb = k / QK8_0;
  138. for (int i = 0; i < nb; i++) {
  139. float amax = 0.0f; // absolute max
  140. for (int j = 0; j < QK8_0; j++) {
  141. const float v = x[i*QK8_0 + j];
  142. amax = MAX(amax, fabsf(v));
  143. }
  144. const float d = amax / ((1 << 7) - 1);
  145. const float id = d ? 1.0f/d : 0.0f;
  146. y[i].d = GGML_FP32_TO_FP16(d);
  147. for (int j = 0; j < QK8_0; ++j) {
  148. const float x0 = x[i*QK8_0 + j]*id;
  149. y[i].qs[j] = roundf(x0);
  150. }
  151. }
  152. }
  153. // reference implementation for deterministic creation of model files
  154. void quantize_row_q8_1_ref(const float * GGML_RESTRICT x, block_q8_1 * GGML_RESTRICT y, int64_t k) {
  155. assert(QK8_1 == 32);
  156. assert(k % QK8_1 == 0);
  157. const int nb = k / QK8_1;
  158. for (int i = 0; i < nb; i++) {
  159. float amax = 0.0f; // absolute max
  160. for (int j = 0; j < QK8_1; j++) {
  161. const float v = x[i*QK8_1 + j];
  162. amax = MAX(amax, fabsf(v));
  163. }
  164. const float d = amax / ((1 << 7) - 1);
  165. const float id = d ? 1.0f/d : 0.0f;
  166. y[i].d = GGML_FP32_TO_FP16(d);
  167. int sum = 0;
  168. for (int j = 0; j < QK8_1/2; ++j) {
  169. const float v0 = x[i*QK8_1 + j]*id;
  170. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  171. y[i].qs[ j] = roundf(v0);
  172. y[i].qs[QK8_1/2 + j] = roundf(v1);
  173. sum += y[i].qs[ j];
  174. sum += y[i].qs[QK8_1/2 + j];
  175. }
  176. y[i].s = GGML_FP32_TO_FP16(sum*d);
  177. }
  178. }
  179. void dequantize_row_q4_0(const block_q4_0 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  180. static const int qk = QK4_0;
  181. assert(k % qk == 0);
  182. const int nb = k / qk;
  183. for (int i = 0; i < nb; i++) {
  184. const float d = GGML_FP16_TO_FP32(x[i].d);
  185. for (int j = 0; j < qk/2; ++j) {
  186. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  187. const int x1 = (x[i].qs[j] >> 4) - 8;
  188. y[i*qk + j + 0 ] = x0*d;
  189. y[i*qk + j + qk/2] = x1*d;
  190. }
  191. }
  192. }
  193. void dequantize_row_q4_1(const block_q4_1 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  194. static const int qk = QK4_1;
  195. assert(k % qk == 0);
  196. const int nb = k / qk;
  197. for (int i = 0; i < nb; i++) {
  198. const float d = GGML_FP16_TO_FP32(x[i].d);
  199. const float m = GGML_FP16_TO_FP32(x[i].m);
  200. for (int j = 0; j < qk/2; ++j) {
  201. const int x0 = (x[i].qs[j] & 0x0F);
  202. const int x1 = (x[i].qs[j] >> 4);
  203. y[i*qk + j + 0 ] = x0*d + m;
  204. y[i*qk + j + qk/2] = x1*d + m;
  205. }
  206. }
  207. }
  208. void dequantize_row_q5_0(const block_q5_0 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  209. static const int qk = QK5_0;
  210. assert(k % qk == 0);
  211. const int nb = k / qk;
  212. for (int i = 0; i < nb; i++) {
  213. const float d = GGML_FP16_TO_FP32(x[i].d);
  214. uint32_t qh;
  215. memcpy(&qh, x[i].qh, sizeof(qh));
  216. for (int j = 0; j < qk/2; ++j) {
  217. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  218. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  219. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  220. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  221. y[i*qk + j + 0 ] = x0*d;
  222. y[i*qk + j + qk/2] = x1*d;
  223. }
  224. }
  225. }
  226. void dequantize_row_q5_1(const block_q5_1 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  227. static const int qk = QK5_1;
  228. assert(k % qk == 0);
  229. const int nb = k / qk;
  230. for (int i = 0; i < nb; i++) {
  231. const float d = GGML_FP16_TO_FP32(x[i].d);
  232. const float m = GGML_FP16_TO_FP32(x[i].m);
  233. uint32_t qh;
  234. memcpy(&qh, x[i].qh, sizeof(qh));
  235. for (int j = 0; j < qk/2; ++j) {
  236. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  237. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  238. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  239. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  240. y[i*qk + j + 0 ] = x0*d + m;
  241. y[i*qk + j + qk/2] = x1*d + m;
  242. }
  243. }
  244. }
  245. void dequantize_row_q8_0(const block_q8_0 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  246. static const int qk = QK8_0;
  247. assert(k % qk == 0);
  248. const int nb = k / qk;
  249. for (int i = 0; i < nb; i++) {
  250. const float d = GGML_FP16_TO_FP32(x[i].d);
  251. for (int j = 0; j < qk; ++j) {
  252. y[i*qk + j] = x[i].qs[j]*d;
  253. }
  254. }
  255. }
  256. //
  257. // 2-6 bit quantization in super-blocks
  258. //
  259. //
  260. // ===================== Helper functions
  261. //
  262. static inline int nearest_int(float fval) {
  263. assert(fabsf(fval) <= 4194303.f);
  264. float val = fval + 12582912.f;
  265. int i; memcpy(&i, &val, sizeof(int));
  266. return (i & 0x007fffff) - 0x00400000;
  267. }
  268. static float make_qx_quants(int n, int nmax, const float * GGML_RESTRICT x, int8_t * GGML_RESTRICT L, int rmse_type,
  269. const float * GGML_RESTRICT qw) {
  270. float max = 0;
  271. float amax = 0;
  272. for (int i = 0; i < n; ++i) {
  273. float ax = fabsf(x[i]);
  274. if (ax > amax) { amax = ax; max = x[i]; }
  275. }
  276. if (amax < GROUP_MAX_EPS) { // all zero
  277. for (int i = 0; i < n; ++i) {
  278. L[i] = 0;
  279. }
  280. return 0.f;
  281. }
  282. float iscale = -nmax / max;
  283. if (rmse_type == 0) {
  284. for (int i = 0; i < n; ++i) {
  285. int l = nearest_int(iscale * x[i]);
  286. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  287. }
  288. return 1/iscale;
  289. }
  290. bool return_early = false;
  291. if (rmse_type < 0) {
  292. rmse_type = -rmse_type;
  293. return_early = true;
  294. }
  295. float sumlx = 0;
  296. float suml2 = 0;
  297. #ifdef HAVE_BUGGY_APPLE_LINKER
  298. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  299. for (volatile int i = 0; i < n; ++i) {
  300. #else
  301. for (int i = 0; i < n; ++i) {
  302. #endif
  303. int l = nearest_int(iscale * x[i]);
  304. l = MAX(-nmax, MIN(nmax-1, l));
  305. L[i] = l + nmax;
  306. 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]));
  307. sumlx += w*x[i]*l;
  308. suml2 += w*l*l;
  309. }
  310. float scale = suml2 ? sumlx/suml2 : 0.0f;
  311. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  312. float best = scale * sumlx;
  313. for (int is = -9; is <= 9; ++is) {
  314. if (is == 0) {
  315. continue;
  316. }
  317. iscale = -(nmax + 0.1f*is) / max;
  318. sumlx = suml2 = 0;
  319. for (int i = 0; i < n; ++i) {
  320. int l = nearest_int(iscale * x[i]);
  321. l = MAX(-nmax, MIN(nmax-1, l));
  322. 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]));
  323. sumlx += w*x[i]*l;
  324. suml2 += w*l*l;
  325. }
  326. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  327. for (int i = 0; i < n; ++i) {
  328. int l = nearest_int(iscale * x[i]);
  329. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  330. }
  331. scale = sumlx/suml2; best = scale*sumlx;
  332. }
  333. }
  334. return scale;
  335. }
  336. static float make_q3_quants(int n, int nmax, const float * GGML_RESTRICT x, int8_t * GGML_RESTRICT L, bool do_rmse) {
  337. float max = 0;
  338. float amax = 0;
  339. for (int i = 0; i < n; ++i) {
  340. float ax = fabsf(x[i]);
  341. if (ax > amax) { amax = ax; max = x[i]; }
  342. }
  343. if (amax < GROUP_MAX_EPS) { // all zero
  344. for (int i = 0; i < n; ++i) { L[i] = 0; }
  345. return 0.f;
  346. }
  347. float iscale = -nmax / max;
  348. if (do_rmse) {
  349. float sumlx = 0;
  350. float suml2 = 0;
  351. for (int i = 0; i < n; ++i) {
  352. int l = nearest_int(iscale * x[i]);
  353. l = MAX(-nmax, MIN(nmax-1, l));
  354. L[i] = l;
  355. float w = x[i]*x[i];
  356. sumlx += w*x[i]*l;
  357. suml2 += w*l*l;
  358. }
  359. for (int itry = 0; itry < 5; ++itry) {
  360. int n_changed = 0;
  361. for (int i = 0; i < n; ++i) {
  362. float w = x[i]*x[i];
  363. float slx = sumlx - w*x[i]*L[i];
  364. if (slx > 0) {
  365. float sl2 = suml2 - w*L[i]*L[i];
  366. int new_l = nearest_int(x[i] * sl2 / slx);
  367. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  368. if (new_l != L[i]) {
  369. slx += w*x[i]*new_l;
  370. sl2 += w*new_l*new_l;
  371. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  372. L[i] = new_l; sumlx = slx; suml2 = sl2;
  373. ++n_changed;
  374. }
  375. }
  376. }
  377. }
  378. if (!n_changed) {
  379. break;
  380. }
  381. }
  382. for (int i = 0; i < n; ++i) {
  383. L[i] += nmax;
  384. }
  385. return sumlx / suml2;
  386. }
  387. for (int i = 0; i < n; ++i) {
  388. int l = nearest_int(iscale * x[i]);
  389. l = MAX(-nmax, MIN(nmax-1, l));
  390. L[i] = l + nmax;
  391. }
  392. return 1/iscale;
  393. }
  394. static float make_qkx1_quants(int n, int nmax, const float * GGML_RESTRICT x, uint8_t * GGML_RESTRICT L, float * GGML_RESTRICT the_min,
  395. int ntry, float alpha) {
  396. float min = x[0];
  397. float max = x[0];
  398. for (int i = 1; i < n; ++i) {
  399. if (x[i] < min) min = x[i];
  400. if (x[i] > max) max = x[i];
  401. }
  402. if (max == min) {
  403. for (int i = 0; i < n; ++i) L[i] = 0;
  404. *the_min = 0;
  405. return 0.f;
  406. }
  407. if (min > 0) min = 0;
  408. float iscale = nmax/(max - min);
  409. float scale = 1/iscale;
  410. for (int itry = 0; itry < ntry; ++itry) {
  411. float sumlx = 0; int suml2 = 0;
  412. bool did_change = false;
  413. for (int i = 0; i < n; ++i) {
  414. int l = nearest_int(iscale*(x[i] - min));
  415. l = MAX(0, MIN(nmax, l));
  416. if (l != L[i]) {
  417. L[i] = l;
  418. did_change = true;
  419. }
  420. sumlx += (x[i] - min)*l;
  421. suml2 += l*l;
  422. }
  423. scale = sumlx/suml2;
  424. float sum = 0;
  425. for (int i = 0; i < n; ++i) {
  426. sum += x[i] - scale*L[i];
  427. }
  428. min = alpha*min + (1 - alpha)*sum/n;
  429. if (min > 0) min = 0;
  430. iscale = 1/scale;
  431. if (!did_change) break;
  432. }
  433. *the_min = -min;
  434. return scale;
  435. }
  436. static float make_qkx2_quants(int n, int nmax, const float * GGML_RESTRICT x, const float * GGML_RESTRICT weights,
  437. uint8_t * GGML_RESTRICT L, float * GGML_RESTRICT the_min, uint8_t * GGML_RESTRICT Laux,
  438. float rmin, float rdelta, int nstep, bool use_mad) {
  439. float min = x[0];
  440. float max = x[0];
  441. float sum_w = weights[0];
  442. float sum_x = sum_w * x[0];
  443. #ifdef HAVE_BUGGY_APPLE_LINKER
  444. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  445. for (volatile int i = 1; i < n; ++i) {
  446. #else
  447. for (int i = 1; i < n; ++i) {
  448. #endif
  449. if (x[i] < min) min = x[i];
  450. if (x[i] > max) max = x[i];
  451. float w = weights[i];
  452. sum_w += w;
  453. sum_x += w * x[i];
  454. }
  455. if (min > 0) min = 0;
  456. if (max == min) {
  457. for (int i = 0; i < n; ++i) L[i] = 0;
  458. *the_min = -min;
  459. return 0.f;
  460. }
  461. float iscale = nmax/(max - min);
  462. float scale = 1/iscale;
  463. float best_mad = 0;
  464. for (int i = 0; i < n; ++i) {
  465. int l = nearest_int(iscale*(x[i] - min));
  466. L[i] = MAX(0, MIN(nmax, l));
  467. float diff = scale * L[i] + min - x[i];
  468. diff = use_mad ? fabsf(diff) : diff * diff;
  469. float w = weights[i];
  470. best_mad += w * diff;
  471. }
  472. if (nstep < 1) {
  473. *the_min = -min;
  474. return scale;
  475. }
  476. for (int is = 0; is <= nstep; ++is) {
  477. iscale = (rmin + rdelta*is + nmax)/(max - min);
  478. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  479. for (int i = 0; i < n; ++i) {
  480. int l = nearest_int(iscale*(x[i] - min));
  481. l = MAX(0, MIN(nmax, l));
  482. Laux[i] = l;
  483. float w = weights[i];
  484. sum_l += w*l;
  485. sum_l2 += w*l*l;
  486. sum_xl += w*l*x[i];
  487. }
  488. float D = sum_w * sum_l2 - sum_l * sum_l;
  489. if (D > 0) {
  490. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  491. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  492. if (this_min > 0) {
  493. this_min = 0;
  494. this_scale = sum_xl / sum_l2;
  495. }
  496. float mad = 0;
  497. for (int i = 0; i < n; ++i) {
  498. float diff = this_scale * Laux[i] + this_min - x[i];
  499. diff = use_mad ? fabsf(diff) : diff * diff;
  500. float w = weights[i];
  501. mad += w * diff;
  502. }
  503. if (mad < best_mad) {
  504. for (int i = 0; i < n; ++i) {
  505. L[i] = Laux[i];
  506. }
  507. best_mad = mad;
  508. scale = this_scale;
  509. min = this_min;
  510. }
  511. }
  512. }
  513. *the_min = -min;
  514. return scale;
  515. }
  516. static inline void get_scale_min_k4(int j, const uint8_t * GGML_RESTRICT q, uint8_t * GGML_RESTRICT d, uint8_t * GGML_RESTRICT m) {
  517. if (j < 4) {
  518. *d = q[j] & 63; *m = q[j + 4] & 63;
  519. } else {
  520. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  521. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  522. }
  523. }
  524. //========================- 2-bit (de)-quantization
  525. void quantize_row_q2_K_ref(const float * GGML_RESTRICT x, block_q2_K * GGML_RESTRICT y, int64_t k) {
  526. assert(k % QK_K == 0);
  527. const int nb = k / QK_K;
  528. uint8_t L[QK_K];
  529. uint8_t Laux[16];
  530. float weights[16];
  531. float mins[QK_K/16];
  532. float scales[QK_K/16];
  533. const float q4scale = 15.f;
  534. for (int i = 0; i < nb; i++) {
  535. float max_scale = 0; // as we are deducting the min, scales are always positive
  536. float max_min = 0;
  537. for (int j = 0; j < QK_K/16; ++j) {
  538. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  539. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  540. float scale = scales[j];
  541. if (scale > max_scale) {
  542. max_scale = scale;
  543. }
  544. float min = mins[j];
  545. if (min > max_min) {
  546. max_min = min;
  547. }
  548. }
  549. if (max_scale > 0) {
  550. float iscale = q4scale/max_scale;
  551. for (int j = 0; j < QK_K/16; ++j) {
  552. int l = nearest_int(iscale*scales[j]);
  553. y[i].scales[j] = l;
  554. }
  555. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  556. } else {
  557. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  558. y[i].d = GGML_FP32_TO_FP16(0.f);
  559. }
  560. if (max_min > 0) {
  561. float iscale = q4scale/max_min;
  562. for (int j = 0; j < QK_K/16; ++j) {
  563. int l = nearest_int(iscale*mins[j]);
  564. y[i].scales[j] |= (l << 4);
  565. }
  566. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  567. } else {
  568. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  569. }
  570. for (int j = 0; j < QK_K/16; ++j) {
  571. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  572. if (!d) continue;
  573. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  574. for (int ii = 0; ii < 16; ++ii) {
  575. int l = nearest_int((x[16*j + ii] + dm)/d);
  576. l = MAX(0, MIN(3, l));
  577. L[16*j + ii] = l;
  578. }
  579. }
  580. for (int j = 0; j < QK_K; j += 128) {
  581. for (int l = 0; l < 32; ++l) {
  582. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  583. }
  584. }
  585. x += QK_K;
  586. }
  587. }
  588. void dequantize_row_q2_K(const block_q2_K * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  589. assert(k % QK_K == 0);
  590. const int nb = k / QK_K;
  591. for (int i = 0; i < nb; i++) {
  592. const float d = GGML_FP16_TO_FP32(x[i].d);
  593. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  594. const uint8_t * q = x[i].qs;
  595. int is = 0;
  596. float dl, ml;
  597. for (int n = 0; n < QK_K; n += 128) {
  598. int shift = 0;
  599. for (int j = 0; j < 4; ++j) {
  600. uint8_t sc = x[i].scales[is++];
  601. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  602. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  603. sc = x[i].scales[is++];
  604. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  605. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  606. shift += 2;
  607. }
  608. q += 32;
  609. }
  610. }
  611. }
  612. static float make_qkx3_quants(int n, int nmax, const float * GGML_RESTRICT x, const float * GGML_RESTRICT weights,
  613. uint8_t * GGML_RESTRICT L, float * GGML_RESTRICT the_min, uint8_t * GGML_RESTRICT Laux,
  614. float rmin, float rdelta, int nstep, bool use_mad) {
  615. float min = x[0];
  616. float max = x[0];
  617. float sum_w = weights ? weights[0] : x[0]*x[0];
  618. float sum_x = sum_w * x[0];
  619. #ifdef HAVE_BUGGY_APPLE_LINKER
  620. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  621. for (volatile int i = 1; i < n; ++i) {
  622. #else
  623. for (int i = 1; i < n; ++i) {
  624. #endif
  625. if (x[i] < min) min = x[i];
  626. if (x[i] > max) max = x[i];
  627. float w = weights ? weights[i] : x[i]*x[i];
  628. sum_w += w;
  629. sum_x += w * x[i];
  630. }
  631. if (min > 0) {
  632. min = 0;
  633. }
  634. if (max <= min) {
  635. memset(L, 0, n);
  636. *the_min = -min;
  637. return 0.f;
  638. }
  639. float iscale = nmax/(max - min);
  640. float scale = 1/iscale;
  641. float best_mad = 0;
  642. for (int i = 0; i < n; ++i) {
  643. int l = nearest_int(iscale*(x[i] - min));
  644. L[i] = MAX(0, MIN(nmax, l));
  645. float diff = scale * L[i] + min - x[i];
  646. diff = use_mad ? fabsf(diff) : diff*diff;
  647. float w = weights ? weights[i] : x[i]*x[i];
  648. best_mad += w * diff;
  649. }
  650. if (nstep < 1) {
  651. *the_min = -min;
  652. return scale;
  653. }
  654. for (int is = 0; is <= nstep; ++is) {
  655. iscale = (rmin + rdelta*is + nmax)/(max - min);
  656. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  657. for (int i = 0; i < n; ++i) {
  658. int l = nearest_int(iscale*(x[i] - min));
  659. l = MAX(0, MIN(nmax, l));
  660. Laux[i] = l;
  661. float w = weights ? weights[i] : x[i]*x[i];
  662. sum_l += w*l;
  663. sum_l2 += w*l*l;
  664. sum_xl += w*l*x[i];
  665. }
  666. float D = sum_w * sum_l2 - sum_l * sum_l;
  667. if (D > 0) {
  668. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  669. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  670. if (this_min > 0) {
  671. this_min = 0;
  672. this_scale = sum_xl / sum_l2;
  673. }
  674. float mad = 0;
  675. for (int i = 0; i < n; ++i) {
  676. float diff = this_scale * Laux[i] + this_min - x[i];
  677. diff = use_mad ? fabsf(diff) : diff*diff;
  678. float w = weights ? weights[i] : x[i]*x[i];
  679. mad += w * diff;
  680. }
  681. if (mad < best_mad) {
  682. for (int i = 0; i < n; ++i) {
  683. L[i] = Laux[i];
  684. }
  685. best_mad = mad;
  686. scale = this_scale;
  687. min = this_min;
  688. }
  689. }
  690. }
  691. *the_min = -min;
  692. return scale;
  693. }
  694. static float make_qp_quants(int n, int nmax, const float * GGML_RESTRICT x, uint8_t * GGML_RESTRICT L, const float * quant_weights) {
  695. float max = 0;
  696. for (int i = 0; i < n; ++i) {
  697. max = MAX(max, x[i]);
  698. }
  699. if (!max) { // all zero
  700. for (int i = 0; i < n; ++i) { L[i] = 0; }
  701. return 0.f;
  702. }
  703. float iscale = nmax / max;
  704. for (int i = 0; i < n; ++i) {
  705. L[i] = nearest_int(iscale * x[i]);
  706. }
  707. float scale = 1/iscale;
  708. float best_mse = 0;
  709. for (int i = 0; i < n; ++i) {
  710. float diff = x[i] - scale*L[i];
  711. float w = quant_weights[i];
  712. best_mse += w*diff*diff;
  713. }
  714. for (int is = -4; is <= 4; ++is) {
  715. if (is == 0) continue;
  716. float iscale_is = (0.1f*is + nmax)/max;
  717. float scale_is = 1/iscale_is;
  718. float mse = 0;
  719. for (int i = 0; i < n; ++i) {
  720. int l = nearest_int(iscale_is*x[i]);
  721. l = MIN(nmax, l);
  722. float diff = x[i] - scale_is*l;
  723. float w = quant_weights[i];
  724. mse += w*diff*diff;
  725. }
  726. if (mse < best_mse) {
  727. best_mse = mse;
  728. iscale = iscale_is;
  729. }
  730. }
  731. float sumlx = 0;
  732. float suml2 = 0;
  733. for (int i = 0; i < n; ++i) {
  734. int l = nearest_int(iscale * x[i]);
  735. l = MIN(nmax, l);
  736. L[i] = l;
  737. float w = quant_weights[i];
  738. sumlx += w*x[i]*l;
  739. suml2 += w*l*l;
  740. }
  741. for (int itry = 0; itry < 5; ++itry) {
  742. int n_changed = 0;
  743. for (int i = 0; i < n; ++i) {
  744. float w = quant_weights[i];
  745. float slx = sumlx - w*x[i]*L[i];
  746. float sl2 = suml2 - w*L[i]*L[i];
  747. if (slx > 0 && sl2 > 0) {
  748. int new_l = nearest_int(x[i] * sl2 / slx);
  749. new_l = MIN(nmax, new_l);
  750. if (new_l != L[i]) {
  751. slx += w*x[i]*new_l;
  752. sl2 += w*new_l*new_l;
  753. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  754. L[i] = new_l; sumlx = slx; suml2 = sl2;
  755. ++n_changed;
  756. }
  757. }
  758. }
  759. }
  760. if (!n_changed) {
  761. break;
  762. }
  763. }
  764. return sumlx/suml2;
  765. }
  766. static void quantize_row_q2_K_impl(const float * GGML_RESTRICT x, block_q2_K * GGML_RESTRICT y, int k, const float * GGML_RESTRICT quant_weights) {
  767. GGML_ASSERT(quant_weights);
  768. assert(k % QK_K == 0);
  769. const int nb = k / QK_K;
  770. const bool requantize = true;
  771. uint8_t L[QK_K];
  772. uint8_t Laux[16];
  773. float mins[QK_K/16];
  774. float scales[QK_K/16];
  775. float sw[QK_K/16];
  776. float weight[16];
  777. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  778. for (int i = 0; i < nb; i++) {
  779. memset(sw, 0, QK_K/16*sizeof(float));
  780. float sumx2 = 0;
  781. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  782. float sigma2 = sumx2/QK_K;
  783. for (int j = 0; j < QK_K/16; ++j) {
  784. const float * GGML_RESTRICT qw = quant_weights + QK_K * i + 16*j;
  785. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  786. for (int l = 0; l < QK_K/16; ++l) sw[j] += weight[l];
  787. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  788. }
  789. float dm, mm;
  790. dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  791. mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  792. y[i].d = GGML_FP32_TO_FP16(dm);
  793. y[i].dmin = GGML_FP32_TO_FP16(mm);
  794. dm = GGML_FP16_TO_FP32(y[i].d);
  795. mm = GGML_FP16_TO_FP32(y[i].dmin);
  796. for (int j = 0; j < QK_K/16; ++j) {
  797. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  798. }
  799. if (requantize) {
  800. for (int j = 0; j < QK_K/16; ++j) {
  801. const float d = dm * (y[i].scales[j] & 0xF);
  802. if (!d) continue;
  803. const float m = mm * (y[i].scales[j] >> 4);
  804. for (int ii = 0; ii < 16; ++ii) {
  805. int l = nearest_int((x[16*j + ii] + m)/d);
  806. l = MAX(0, MIN(3, l));
  807. L[16*j + ii] = l;
  808. }
  809. }
  810. }
  811. for (int j = 0; j < QK_K; j += 128) {
  812. for (int l = 0; l < 32; ++l) {
  813. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  814. }
  815. }
  816. x += QK_K;
  817. }
  818. }
  819. size_t quantize_q2_K(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  820. size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  821. if (!quant_weights) {
  822. quantize_row_q2_K_ref(src, dst, (int64_t)nrow*n_per_row);
  823. }
  824. else {
  825. char * qrow = (char *)dst;
  826. for (int64_t row = 0; row < nrow; ++row) {
  827. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  828. src += n_per_row;
  829. qrow += row_size;
  830. }
  831. }
  832. return nrow * row_size;
  833. }
  834. //========================= 3-bit (de)-quantization
  835. void quantize_row_q3_K_ref(const float * GGML_RESTRICT x, block_q3_K * GGML_RESTRICT y, int64_t k) {
  836. assert(k % QK_K == 0);
  837. const int nb = k / QK_K;
  838. int8_t L[QK_K];
  839. float scales[QK_K / 16];
  840. for (int i = 0; i < nb; i++) {
  841. float max_scale = 0;
  842. float amax = 0;
  843. for (int j = 0; j < QK_K/16; ++j) {
  844. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  845. float scale = fabsf(scales[j]);
  846. if (scale > amax) {
  847. amax = scale; max_scale = scales[j];
  848. }
  849. }
  850. memset(y[i].scales, 0, 12);
  851. if (max_scale) {
  852. float iscale = -32.f/max_scale;
  853. for (int j = 0; j < QK_K/16; ++j) {
  854. int8_t l = nearest_int(iscale*scales[j]);
  855. l = MAX(-32, MIN(31, l)) + 32;
  856. if (j < 8) {
  857. y[i].scales[j] = l & 0xF;
  858. } else {
  859. y[i].scales[j-8] |= ((l & 0xF) << 4);
  860. }
  861. l >>= 4;
  862. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  863. }
  864. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  865. } else {
  866. y[i].d = GGML_FP32_TO_FP16(0.f);
  867. }
  868. int8_t sc;
  869. for (int j = 0; j < QK_K/16; ++j) {
  870. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  871. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  872. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  873. if (!d) {
  874. continue;
  875. }
  876. for (int ii = 0; ii < 16; ++ii) {
  877. int l = nearest_int(x[16*j + ii]/d);
  878. l = MAX(-4, MIN(3, l));
  879. L[16*j + ii] = l + 4;
  880. }
  881. }
  882. memset(y[i].hmask, 0, QK_K/8);
  883. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  884. int m = 0;
  885. uint8_t hm = 1;
  886. for (int j = 0; j < QK_K; ++j) {
  887. if (L[j] > 3) {
  888. y[i].hmask[m] |= hm;
  889. L[j] -= 4;
  890. }
  891. if (++m == QK_K/8) {
  892. m = 0; hm <<= 1;
  893. }
  894. }
  895. for (int j = 0; j < QK_K; j += 128) {
  896. for (int l = 0; l < 32; ++l) {
  897. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  898. }
  899. }
  900. x += QK_K;
  901. }
  902. }
  903. void dequantize_row_q3_K(const block_q3_K * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  904. assert(k % QK_K == 0);
  905. const int nb = k / QK_K;
  906. const uint32_t kmask1 = 0x03030303;
  907. const uint32_t kmask2 = 0x0f0f0f0f;
  908. uint32_t aux[4];
  909. const int8_t * scales = (const int8_t*)aux;
  910. for (int i = 0; i < nb; i++) {
  911. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  912. const uint8_t * GGML_RESTRICT q = x[i].qs;
  913. const uint8_t * GGML_RESTRICT hm = x[i].hmask;
  914. uint8_t m = 1;
  915. memcpy(aux, x[i].scales, 12);
  916. uint32_t tmp = aux[2];
  917. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  918. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  919. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  920. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  921. int is = 0;
  922. float dl;
  923. for (int n = 0; n < QK_K; n += 128) {
  924. int shift = 0;
  925. for (int j = 0; j < 4; ++j) {
  926. dl = d_all * (scales[is++] - 32);
  927. for (int l = 0; l < 16; ++l) {
  928. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  929. }
  930. dl = d_all * (scales[is++] - 32);
  931. for (int l = 0; l < 16; ++l) {
  932. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  933. }
  934. shift += 2;
  935. m <<= 1;
  936. }
  937. q += 32;
  938. }
  939. }
  940. }
  941. static void quantize_row_q3_K_impl(const float * GGML_RESTRICT x, block_q3_K * GGML_RESTRICT y, int64_t n_per_row, const float * GGML_RESTRICT quant_weights) {
  942. assert(n_per_row % QK_K == 0);
  943. const int nb = n_per_row / QK_K;
  944. int8_t L[QK_K];
  945. float scales[QK_K / 16];
  946. float weight[16];
  947. float sw[QK_K / 16];
  948. int8_t Ls[QK_K / 16];
  949. for (int i = 0; i < nb; i++) {
  950. float sumx2 = 0;
  951. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  952. float sigma2 = 2*sumx2/QK_K;
  953. for (int j = 0; j < QK_K/16; ++j) {
  954. if (quant_weights) {
  955. const float * qw = quant_weights + QK_K * i + 16*j;
  956. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  957. } else {
  958. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  959. }
  960. float sumw = 0;
  961. for (int l = 0; l < 16; ++l) sumw += weight[l];
  962. sw[j] = sumw;
  963. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  964. }
  965. memset(y[i].scales, 0, 12);
  966. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  967. for (int j = 0; j < QK_K/16; ++j) {
  968. int l = Ls[j];
  969. if (j < 8) {
  970. y[i].scales[j] = l & 0xF;
  971. } else {
  972. y[i].scales[j-8] |= ((l & 0xF) << 4);
  973. }
  974. l >>= 4;
  975. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  976. }
  977. y[i].d = GGML_FP32_TO_FP16(d_block);
  978. int8_t sc;
  979. for (int j = 0; j < QK_K/16; ++j) {
  980. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  981. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  982. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  983. if (!d) {
  984. continue;
  985. }
  986. for (int ii = 0; ii < 16; ++ii) {
  987. int l = nearest_int(x[16*j + ii]/d);
  988. l = MAX(-4, MIN(3, l));
  989. L[16*j + ii] = l + 4;
  990. }
  991. }
  992. memset(y[i].hmask, 0, QK_K/8);
  993. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  994. int m = 0;
  995. uint8_t hm = 1;
  996. for (int j = 0; j < QK_K; ++j) {
  997. if (L[j] > 3) {
  998. y[i].hmask[m] |= hm;
  999. L[j] -= 4;
  1000. }
  1001. if (++m == QK_K/8) {
  1002. m = 0; hm <<= 1;
  1003. }
  1004. }
  1005. for (int j = 0; j < QK_K; j += 128) {
  1006. for (int l = 0; l < 32; ++l) {
  1007. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1008. }
  1009. }
  1010. x += QK_K;
  1011. }
  1012. }
  1013. size_t quantize_q3_K(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1014. size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  1015. if (!quant_weights) {
  1016. quantize_row_q3_K_ref(src, dst, (int64_t)nrow*n_per_row);
  1017. }
  1018. else {
  1019. char * qrow = (char *)dst;
  1020. for (int64_t row = 0; row < nrow; ++row) {
  1021. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  1022. src += n_per_row;
  1023. qrow += row_size;
  1024. }
  1025. }
  1026. return nrow * row_size;
  1027. }
  1028. // ====================== 4-bit (de)-quantization
  1029. void quantize_row_q4_K_ref(const float * GGML_RESTRICT x, block_q4_K * GGML_RESTRICT y, int64_t k) {
  1030. assert(k % QK_K == 0);
  1031. const int nb = k / QK_K;
  1032. uint8_t L[QK_K];
  1033. uint8_t Laux[32];
  1034. float weights[32];
  1035. float mins[QK_K/32];
  1036. float scales[QK_K/32];
  1037. for (int i = 0; i < nb; i++) {
  1038. float max_scale = 0; // as we are deducting the min, scales are always positive
  1039. float max_min = 0;
  1040. for (int j = 0; j < QK_K/32; ++j) {
  1041. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1042. float sum_x2 = 0;
  1043. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1044. float av_x = sqrtf(sum_x2/32);
  1045. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1046. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1047. float scale = scales[j];
  1048. if (scale > max_scale) {
  1049. max_scale = scale;
  1050. }
  1051. float min = mins[j];
  1052. if (min > max_min) {
  1053. max_min = min;
  1054. }
  1055. }
  1056. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1057. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1058. for (int j = 0; j < QK_K/32; ++j) {
  1059. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1060. uint8_t lm = nearest_int(inv_min*mins[j]);
  1061. ls = MIN(63, ls);
  1062. lm = MIN(63, lm);
  1063. if (j < 4) {
  1064. y[i].scales[j] = ls;
  1065. y[i].scales[j+4] = lm;
  1066. } else {
  1067. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1068. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1069. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1070. }
  1071. }
  1072. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1073. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1074. uint8_t sc, m;
  1075. for (int j = 0; j < QK_K/32; ++j) {
  1076. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1077. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1078. if (!d) continue;
  1079. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1080. for (int ii = 0; ii < 32; ++ii) {
  1081. int l = nearest_int((x[32*j + ii] + dm)/d);
  1082. l = MAX(0, MIN(15, l));
  1083. L[32*j + ii] = l;
  1084. }
  1085. }
  1086. uint8_t * q = y[i].qs;
  1087. for (int j = 0; j < QK_K; j += 64) {
  1088. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1089. q += 32;
  1090. }
  1091. x += QK_K;
  1092. }
  1093. }
  1094. void dequantize_row_q4_K(const block_q4_K * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  1095. assert(k % QK_K == 0);
  1096. const int nb = k / QK_K;
  1097. for (int i = 0; i < nb; i++) {
  1098. const uint8_t * q = x[i].qs;
  1099. const float d = GGML_FP16_TO_FP32(x[i].d);
  1100. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1101. int is = 0;
  1102. uint8_t sc, m;
  1103. for (int j = 0; j < QK_K; j += 64) {
  1104. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1105. const float d1 = d * sc; const float m1 = min * m;
  1106. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1107. const float d2 = d * sc; const float m2 = min * m;
  1108. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  1109. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  1110. q += 32; is += 2;
  1111. }
  1112. }
  1113. }
  1114. static void quantize_row_q4_K_impl(const float * GGML_RESTRICT x, block_q4_K * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
  1115. assert(n_per_row % QK_K == 0);
  1116. const int64_t nb = n_per_row / QK_K;
  1117. uint8_t L[QK_K];
  1118. uint8_t Laux[32];
  1119. uint8_t Ls[QK_K/32];
  1120. uint8_t Lm[QK_K/32];
  1121. float weights[32];
  1122. float sw[QK_K/32];
  1123. float mins[QK_K/32];
  1124. float scales[QK_K/32];
  1125. for (int i = 0; i < nb; i++) {
  1126. float sum_x2 = 0;
  1127. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  1128. float sigma2 = 2*sum_x2/QK_K;
  1129. float av_x = sqrtf(sigma2);
  1130. for (int j = 0; j < QK_K/32; ++j) {
  1131. if (quant_weights) {
  1132. const float * qw = quant_weights + QK_K*i + 32*j;
  1133. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  1134. } else {
  1135. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1136. }
  1137. float sumw = 0;
  1138. for (int l = 0; l < 32; ++l) sumw += weights[l];
  1139. sw[j] = sumw;
  1140. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1141. }
  1142. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  1143. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  1144. for (int j = 0; j < QK_K/32; ++j) {
  1145. uint8_t ls = Ls[j];
  1146. uint8_t lm = Lm[j];
  1147. if (j < 4) {
  1148. y[i].scales[j] = ls;
  1149. y[i].scales[j+4] = lm;
  1150. } else {
  1151. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1152. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1153. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1154. }
  1155. }
  1156. y[i].d = GGML_FP32_TO_FP16(d_block);
  1157. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  1158. uint8_t sc, m;
  1159. for (int j = 0; j < QK_K/32; ++j) {
  1160. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1161. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1162. if (!d) continue;
  1163. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1164. for (int ii = 0; ii < 32; ++ii) {
  1165. int l = nearest_int((x[32*j + ii] + dm)/d);
  1166. l = MAX(0, MIN(15, l));
  1167. L[32*j + ii] = l;
  1168. }
  1169. }
  1170. uint8_t * q = y[i].qs;
  1171. for (int j = 0; j < QK_K; j += 64) {
  1172. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1173. q += 32;
  1174. }
  1175. x += QK_K;
  1176. }
  1177. }
  1178. size_t quantize_q4_K(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1179. size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  1180. if (!quant_weights) {
  1181. quantize_row_q4_K_ref(src, dst, (int64_t)nrow*n_per_row);
  1182. }
  1183. else {
  1184. char * qrow = (char *)dst;
  1185. for (int64_t row = 0; row < nrow; ++row) {
  1186. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  1187. src += n_per_row;
  1188. qrow += row_size;
  1189. }
  1190. }
  1191. return nrow * row_size;
  1192. }
  1193. // ====================== 5-bit (de)-quantization
  1194. void quantize_row_q5_K_ref(const float * GGML_RESTRICT x, block_q5_K * GGML_RESTRICT y, int64_t k) {
  1195. assert(k % QK_K == 0);
  1196. const int64_t nb = k / QK_K;
  1197. uint8_t L[QK_K];
  1198. float mins[QK_K/32];
  1199. float scales[QK_K/32];
  1200. float weights[32];
  1201. uint8_t Laux[32];
  1202. for (int i = 0; i < nb; i++) {
  1203. float max_scale = 0; // as we are deducting the min, scales are always positive
  1204. float max_min = 0;
  1205. for (int j = 0; j < QK_K/32; ++j) {
  1206. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1207. float sum_x2 = 0;
  1208. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1209. float av_x = sqrtf(sum_x2/32);
  1210. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1211. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  1212. float scale = scales[j];
  1213. if (scale > max_scale) {
  1214. max_scale = scale;
  1215. }
  1216. float min = mins[j];
  1217. if (min > max_min) {
  1218. max_min = min;
  1219. }
  1220. }
  1221. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1222. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1223. for (int j = 0; j < QK_K/32; ++j) {
  1224. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1225. uint8_t lm = nearest_int(inv_min*mins[j]);
  1226. ls = MIN(63, ls);
  1227. lm = MIN(63, lm);
  1228. if (j < 4) {
  1229. y[i].scales[j] = ls;
  1230. y[i].scales[j+4] = lm;
  1231. } else {
  1232. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1233. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1234. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1235. }
  1236. }
  1237. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1238. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1239. uint8_t sc, m;
  1240. for (int j = 0; j < QK_K/32; ++j) {
  1241. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1242. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1243. if (!d) continue;
  1244. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1245. for (int ii = 0; ii < 32; ++ii) {
  1246. int l = nearest_int((x[32*j + ii] + dm)/d);
  1247. l = MAX(0, MIN(31, l));
  1248. L[32*j + ii] = l;
  1249. }
  1250. }
  1251. uint8_t * GGML_RESTRICT qh = y[i].qh;
  1252. uint8_t * GGML_RESTRICT ql = y[i].qs;
  1253. memset(qh, 0, QK_K/8);
  1254. uint8_t m1 = 1, m2 = 2;
  1255. for (int n = 0; n < QK_K; n += 64) {
  1256. for (int j = 0; j < 32; ++j) {
  1257. int l1 = L[n + j];
  1258. if (l1 > 15) {
  1259. l1 -= 16; qh[j] |= m1;
  1260. }
  1261. int l2 = L[n + j + 32];
  1262. if (l2 > 15) {
  1263. l2 -= 16; qh[j] |= m2;
  1264. }
  1265. ql[j] = l1 | (l2 << 4);
  1266. }
  1267. m1 <<= 2; m2 <<= 2;
  1268. ql += 32;
  1269. }
  1270. x += QK_K;
  1271. }
  1272. }
  1273. void dequantize_row_q5_K(const block_q5_K * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  1274. assert(k % QK_K == 0);
  1275. const int64_t nb = k / QK_K;
  1276. for (int i = 0; i < nb; i++) {
  1277. const uint8_t * ql = x[i].qs;
  1278. const uint8_t * qh = x[i].qh;
  1279. const float d = GGML_FP16_TO_FP32(x[i].d);
  1280. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1281. int is = 0;
  1282. uint8_t sc, m;
  1283. uint8_t u1 = 1, u2 = 2;
  1284. for (int j = 0; j < QK_K; j += 64) {
  1285. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1286. const float d1 = d * sc; const float m1 = min * m;
  1287. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1288. const float d2 = d * sc; const float m2 = min * m;
  1289. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  1290. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  1291. ql += 32; is += 2;
  1292. u1 <<= 2; u2 <<= 2;
  1293. }
  1294. }
  1295. }
  1296. static void quantize_row_q5_K_impl(const float * GGML_RESTRICT x, block_q5_K * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
  1297. assert(n_per_row % QK_K == 0);
  1298. const int64_t nb = n_per_row / QK_K;
  1299. uint8_t L[QK_K];
  1300. uint8_t Laux[32];
  1301. uint8_t Ls[QK_K/32];
  1302. uint8_t Lm[QK_K/32];
  1303. float mins[QK_K/32];
  1304. float scales[QK_K/32];
  1305. float sw[QK_K/32];
  1306. float weights[32];
  1307. for (int i = 0; i < nb; i++) {
  1308. float sum_x2 = 0;
  1309. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  1310. float sigma2 = 2*sum_x2/QK_K;
  1311. float av_x = sqrtf(sigma2);
  1312. for (int j = 0; j < QK_K/32; ++j) {
  1313. if (quant_weights) {
  1314. const float * qw = quant_weights + QK_K*i + 32*j;
  1315. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  1316. } else {
  1317. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1318. }
  1319. float sumw = 0;
  1320. for (int l = 0; l < 32; ++l) sumw += weights[l];
  1321. sw[j] = sumw;
  1322. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1323. }
  1324. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  1325. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  1326. for (int j = 0; j < QK_K/32; ++j) {
  1327. uint8_t ls = Ls[j];
  1328. uint8_t lm = Lm[j];
  1329. ls = MIN(63, ls);
  1330. lm = MIN(63, lm);
  1331. if (j < 4) {
  1332. y[i].scales[j] = ls;
  1333. y[i].scales[j+4] = lm;
  1334. } else {
  1335. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1336. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1337. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1338. }
  1339. }
  1340. y[i].d = GGML_FP32_TO_FP16(d_block);
  1341. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  1342. uint8_t sc, m;
  1343. for (int j = 0; j < QK_K/32; ++j) {
  1344. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1345. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1346. if (!d) continue;
  1347. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1348. for (int ii = 0; ii < 32; ++ii) {
  1349. int l = nearest_int((x[32*j + ii] + dm)/d);
  1350. l = MAX(0, MIN(31, l));
  1351. L[32*j + ii] = l;
  1352. }
  1353. }
  1354. uint8_t * GGML_RESTRICT qh = y[i].qh;
  1355. uint8_t * GGML_RESTRICT ql = y[i].qs;
  1356. memset(qh, 0, QK_K/8);
  1357. uint8_t m1 = 1, m2 = 2;
  1358. for (int n = 0; n < QK_K; n += 64) {
  1359. for (int j = 0; j < 32; ++j) {
  1360. int l1 = L[n + j];
  1361. if (l1 > 15) {
  1362. l1 -= 16; qh[j] |= m1;
  1363. }
  1364. int l2 = L[n + j + 32];
  1365. if (l2 > 15) {
  1366. l2 -= 16; qh[j] |= m2;
  1367. }
  1368. ql[j] = l1 | (l2 << 4);
  1369. }
  1370. m1 <<= 2; m2 <<= 2;
  1371. ql += 32;
  1372. }
  1373. x += QK_K;
  1374. }
  1375. }
  1376. size_t quantize_q5_K(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1377. size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  1378. if (!quant_weights) {
  1379. quantize_row_q5_K_ref(src, dst, (int64_t)nrow*n_per_row);
  1380. }
  1381. else {
  1382. char * qrow = (char *)dst;
  1383. for (int64_t row = 0; row < nrow; ++row) {
  1384. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  1385. src += n_per_row;
  1386. qrow += row_size;
  1387. }
  1388. }
  1389. return nrow * row_size;
  1390. }
  1391. // ====================== 6-bit (de)-quantization
  1392. void quantize_row_q6_K_ref(const float * GGML_RESTRICT x, block_q6_K * GGML_RESTRICT y, int64_t k) {
  1393. assert(k % QK_K == 0);
  1394. const int64_t nb = k / QK_K;
  1395. int8_t L[QK_K];
  1396. float scales[QK_K/16];
  1397. for (int i = 0; i < nb; i++) {
  1398. float max_scale = 0;
  1399. float max_abs_scale = 0;
  1400. for (int ib = 0; ib < QK_K/16; ++ib) {
  1401. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  1402. scales[ib] = scale;
  1403. const float abs_scale = fabsf(scale);
  1404. if (abs_scale > max_abs_scale) {
  1405. max_abs_scale = abs_scale;
  1406. max_scale = scale;
  1407. }
  1408. }
  1409. if (max_abs_scale < GROUP_MAX_EPS) {
  1410. memset(&y[i], 0, sizeof(block_q6_K));
  1411. y[i].d = GGML_FP32_TO_FP16(0.f);
  1412. x += QK_K;
  1413. continue;
  1414. }
  1415. float iscale = -128.f/max_scale;
  1416. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1417. for (int ib = 0; ib < QK_K/16; ++ib) {
  1418. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  1419. }
  1420. for (int j = 0; j < QK_K/16; ++j) {
  1421. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  1422. if (!d) {
  1423. continue;
  1424. }
  1425. for (int ii = 0; ii < 16; ++ii) {
  1426. int l = nearest_int(x[16*j + ii]/d);
  1427. l = MAX(-32, MIN(31, l));
  1428. L[16*j + ii] = l + 32;
  1429. }
  1430. }
  1431. uint8_t * GGML_RESTRICT ql = y[i].ql;
  1432. uint8_t * GGML_RESTRICT qh = y[i].qh;
  1433. for (int j = 0; j < QK_K; j += 128) {
  1434. for (int l = 0; l < 32; ++l) {
  1435. const uint8_t q1 = L[j + l + 0] & 0xF;
  1436. const uint8_t q2 = L[j + l + 32] & 0xF;
  1437. const uint8_t q3 = L[j + l + 64] & 0xF;
  1438. const uint8_t q4 = L[j + l + 96] & 0xF;
  1439. ql[l+ 0] = q1 | (q3 << 4);
  1440. ql[l+32] = q2 | (q4 << 4);
  1441. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  1442. }
  1443. ql += 64;
  1444. qh += 32;
  1445. }
  1446. x += QK_K;
  1447. }
  1448. }
  1449. void dequantize_row_q6_K(const block_q6_K * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  1450. assert(k % QK_K == 0);
  1451. const int64_t nb = k / QK_K;
  1452. for (int i = 0; i < nb; i++) {
  1453. const float d = GGML_FP16_TO_FP32(x[i].d);
  1454. const uint8_t * GGML_RESTRICT ql = x[i].ql;
  1455. const uint8_t * GGML_RESTRICT qh = x[i].qh;
  1456. const int8_t * GGML_RESTRICT sc = x[i].scales;
  1457. for (int n = 0; n < QK_K; n += 128) {
  1458. for (int l = 0; l < 32; ++l) {
  1459. int is = l/16;
  1460. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  1461. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  1462. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  1463. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  1464. y[l + 0] = d * sc[is + 0] * q1;
  1465. y[l + 32] = d * sc[is + 2] * q2;
  1466. y[l + 64] = d * sc[is + 4] * q3;
  1467. y[l + 96] = d * sc[is + 6] * q4;
  1468. }
  1469. y += 128;
  1470. ql += 64;
  1471. qh += 32;
  1472. sc += 8;
  1473. }
  1474. }
  1475. }
  1476. static void quantize_row_q6_K_impl(const float * GGML_RESTRICT x, block_q6_K * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
  1477. assert(n_per_row % QK_K == 0);
  1478. const int64_t nb = n_per_row / QK_K;
  1479. int8_t L[QK_K];
  1480. float scales[QK_K/16];
  1481. //float weights[16];
  1482. for (int i = 0; i < nb; i++) {
  1483. //float sum_x2 = 0;
  1484. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  1485. //float sigma2 = sum_x2/QK_K;
  1486. float max_scale = 0;
  1487. float max_abs_scale = 0;
  1488. for (int ib = 0; ib < QK_K/16; ++ib) {
  1489. float scale;
  1490. if (quant_weights) {
  1491. const float * qw = quant_weights + QK_K*i + 16*ib;
  1492. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  1493. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  1494. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  1495. } else {
  1496. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  1497. }
  1498. scales[ib] = scale;
  1499. const float abs_scale = fabsf(scale);
  1500. if (abs_scale > max_abs_scale) {
  1501. max_abs_scale = abs_scale;
  1502. max_scale = scale;
  1503. }
  1504. }
  1505. if (max_abs_scale < GROUP_MAX_EPS) {
  1506. memset(&y[i], 0, sizeof(block_q6_K));
  1507. y[i].d = GGML_FP32_TO_FP16(0.f);
  1508. x += QK_K;
  1509. continue;
  1510. }
  1511. float iscale = -128.f/max_scale;
  1512. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1513. for (int ib = 0; ib < QK_K/16; ++ib) {
  1514. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  1515. }
  1516. for (int j = 0; j < QK_K/16; ++j) {
  1517. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  1518. if (!d) {
  1519. continue;
  1520. }
  1521. for (int ii = 0; ii < 16; ++ii) {
  1522. int l = nearest_int(x[16*j + ii]/d);
  1523. l = MAX(-32, MIN(31, l));
  1524. L[16*j + ii] = l + 32;
  1525. }
  1526. }
  1527. uint8_t * GGML_RESTRICT ql = y[i].ql;
  1528. uint8_t * GGML_RESTRICT qh = y[i].qh;
  1529. for (int j = 0; j < QK_K; j += 128) {
  1530. for (int l = 0; l < 32; ++l) {
  1531. const uint8_t q1 = L[j + l + 0] & 0xF;
  1532. const uint8_t q2 = L[j + l + 32] & 0xF;
  1533. const uint8_t q3 = L[j + l + 64] & 0xF;
  1534. const uint8_t q4 = L[j + l + 96] & 0xF;
  1535. ql[l+ 0] = q1 | (q3 << 4);
  1536. ql[l+32] = q2 | (q4 << 4);
  1537. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  1538. }
  1539. ql += 64;
  1540. qh += 32;
  1541. }
  1542. x += QK_K;
  1543. }
  1544. }
  1545. size_t quantize_q6_K(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1546. size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  1547. if (!quant_weights) {
  1548. quantize_row_q6_K_ref(src, dst, (int64_t)nrow*n_per_row);
  1549. }
  1550. else {
  1551. char * qrow = (char *)dst;
  1552. for (int64_t row = 0; row < nrow; ++row) {
  1553. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  1554. src += n_per_row;
  1555. qrow += row_size;
  1556. }
  1557. }
  1558. return nrow * row_size;
  1559. }
  1560. static void quantize_row_q4_0_impl(const float * GGML_RESTRICT x, block_q4_0 * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
  1561. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  1562. if (!quant_weights) {
  1563. quantize_row_q4_0_ref(x, y, n_per_row);
  1564. return;
  1565. }
  1566. float weight[QK4_0];
  1567. int8_t L[QK4_0];
  1568. float sum_x2 = 0;
  1569. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  1570. float sigma2 = sum_x2/n_per_row;
  1571. const int64_t nb = n_per_row/QK4_0;
  1572. for (int ib = 0; ib < nb; ++ib) {
  1573. const float * xb = x + QK4_0 * ib;
  1574. const float * qw = quant_weights + QK4_0 * ib;
  1575. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  1576. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  1577. y[ib].d = GGML_FP32_TO_FP16(d);
  1578. for (int j = 0; j < 16; ++j) {
  1579. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  1580. }
  1581. }
  1582. }
  1583. size_t quantize_q4_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1584. if (!quant_weights) {
  1585. quantize_row_q4_0_ref(src, dst, (int64_t)nrow*n_per_row);
  1586. return nrow * ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  1587. }
  1588. size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  1589. char * qrow = (char *)dst;
  1590. for (int64_t row = 0; row < nrow; ++row) {
  1591. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  1592. src += n_per_row;
  1593. qrow += row_size;
  1594. }
  1595. return nrow * row_size;
  1596. }
  1597. static void quantize_row_q4_1_impl(const float * GGML_RESTRICT x, block_q4_1 * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
  1598. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  1599. if (!quant_weights) {
  1600. quantize_row_q4_1_ref(x, y, n_per_row);
  1601. return;
  1602. }
  1603. float weight[QK4_1];
  1604. uint8_t L[QK4_1], Laux[QK4_1];
  1605. float sum_x2 = 0;
  1606. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  1607. float sigma2 = sum_x2/n_per_row;
  1608. const int64_t nb = n_per_row/QK4_1;
  1609. for (int ib = 0; ib < nb; ++ib) {
  1610. const float * xb = x + QK4_1 * ib;
  1611. const float * qw = quant_weights + QK4_1 * ib;
  1612. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  1613. float min;
  1614. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  1615. y[ib].d = GGML_FP32_TO_FP16(d);
  1616. y[ib].m = GGML_FP32_TO_FP16(-min);
  1617. for (int j = 0; j < 16; ++j) {
  1618. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  1619. }
  1620. }
  1621. }
  1622. size_t quantize_q4_1(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1623. if (!quant_weights) {
  1624. quantize_row_q4_1_ref(src, dst, (int64_t)nrow*n_per_row);
  1625. return nrow * ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  1626. }
  1627. size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  1628. char * qrow = (char *)dst;
  1629. for (int64_t row = 0; row < nrow; ++row) {
  1630. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  1631. src += n_per_row;
  1632. qrow += row_size;
  1633. }
  1634. return nrow * row_size;
  1635. }
  1636. static void quantize_row_q5_0_impl(const float * GGML_RESTRICT x, block_q5_0 * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
  1637. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  1638. if (!quant_weights) {
  1639. quantize_row_q5_0_ref(x, y, n_per_row);
  1640. return;
  1641. }
  1642. float weight[QK5_0];
  1643. int8_t L[QK5_0];
  1644. float sum_x2 = 0;
  1645. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  1646. float sigma2 = sum_x2/n_per_row;
  1647. const int64_t nb = n_per_row/QK5_0;
  1648. for (int ib = 0; ib < nb; ++ib) {
  1649. const float * xb = x + QK5_0 * ib;
  1650. const float * qw = quant_weights + QK5_0 * ib;
  1651. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  1652. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  1653. y[ib].d = GGML_FP32_TO_FP16(d);
  1654. uint32_t qh = 0;
  1655. for (int j = 0; j < 16; ++j) {
  1656. const uint8_t xi0 = L[j];
  1657. const uint8_t xi1 = L[j+16];
  1658. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  1659. // get the 5-th bit and store it in qh at the right position
  1660. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  1661. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  1662. }
  1663. memcpy(&y[ib].qh, &qh, sizeof(qh));
  1664. }
  1665. }
  1666. size_t quantize_q5_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1667. if (!quant_weights) {
  1668. quantize_row_q5_0_ref(src, dst, (int64_t)nrow*n_per_row);
  1669. return nrow * ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  1670. }
  1671. size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  1672. char * qrow = (char *)dst;
  1673. for (int64_t row = 0; row < nrow; ++row) {
  1674. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  1675. src += n_per_row;
  1676. qrow += row_size;
  1677. }
  1678. return nrow * row_size;
  1679. }
  1680. static void quantize_row_q5_1_impl(const float * GGML_RESTRICT x, block_q5_1 * GGML_RESTRICT y, int64_t n_per_row, const float * quant_weights) {
  1681. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  1682. if (!quant_weights) {
  1683. quantize_row_q5_1_ref(x, y, n_per_row);
  1684. return;
  1685. }
  1686. float weight[QK5_1];
  1687. uint8_t L[QK5_1], Laux[QK5_1];
  1688. float sum_x2 = 0;
  1689. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  1690. float sigma2 = sum_x2/n_per_row;
  1691. const int64_t nb = n_per_row/QK5_1;
  1692. for (int ib = 0; ib < nb; ++ib) {
  1693. const float * xb = x + QK5_1 * ib;
  1694. const float * qw = quant_weights + QK5_1 * ib;
  1695. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  1696. float min;
  1697. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  1698. y[ib].d = GGML_FP32_TO_FP16(d);
  1699. y[ib].m = GGML_FP32_TO_FP16(-min);
  1700. uint32_t qh = 0;
  1701. for (int j = 0; j < 16; ++j) {
  1702. const uint8_t xi0 = L[j];
  1703. const uint8_t xi1 = L[j+16];
  1704. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  1705. // get the 5-th bit and store it in qh at the right position
  1706. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  1707. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  1708. }
  1709. memcpy(&y[ib].qh, &qh, sizeof(qh));
  1710. }
  1711. }
  1712. size_t quantize_q5_1(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1713. if (!quant_weights) {
  1714. quantize_row_q5_1_ref(src, dst, (int64_t)nrow*n_per_row);
  1715. return nrow * ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  1716. }
  1717. size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  1718. char * qrow = (char *)dst;
  1719. for (int64_t row = 0; row < nrow; ++row) {
  1720. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  1721. src += n_per_row;
  1722. qrow += row_size;
  1723. }
  1724. return nrow * row_size;
  1725. }
  1726. size_t quantize_q8_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1727. (void)quant_weights; // not used
  1728. const size_t row_size = ggml_row_size(GGML_TYPE_Q8_0, n_per_row);
  1729. quantize_row_q8_0_ref(src, dst, (int64_t)nrow*n_per_row);
  1730. return nrow * row_size;
  1731. }
  1732. // ====================== Ternary (de)-quantization (BitNet b1.58 and TriLMs)
  1733. void quantize_row_tq1_0_ref(const float * GGML_RESTRICT x, block_tq1_0 * GGML_RESTRICT y, int64_t k) {
  1734. assert(k % QK_K == 0);
  1735. const int64_t nb = k / QK_K;
  1736. for (int64_t i = 0; i < nb; i++) {
  1737. float amax = 0.0f; // absolute max
  1738. for (int j = 0; j < QK_K; j++) {
  1739. const float v = x[j];
  1740. amax = MAX(amax, fabsf(v));
  1741. }
  1742. const float d = amax;
  1743. const float id = d ? 1.0f/d : 0.0f;
  1744. y[i].d = GGML_FP32_TO_FP16(d);
  1745. // 5 elements per byte, along 32 bytes
  1746. for (size_t j = 0; j < sizeof(y->qs) - sizeof(y->qs) % 32; j += 32) {
  1747. for (size_t m = 0; m < 32; ++m) {
  1748. uint8_t q = 0;
  1749. for (size_t n = 0; n < 5; ++n) {
  1750. int xi = lroundf(x[m + n*32] * id) + 1; // -1, 0, 1 -> 0, 1, 2
  1751. q *= 3;
  1752. q += xi;
  1753. }
  1754. // ceiling division (243 == pow(3, 5))
  1755. q = ((uint16_t)q * 256 + (243 - 1)) / 243;
  1756. y[i].qs[j + m] = q;
  1757. }
  1758. x += 5*32;
  1759. }
  1760. // along 16 bytes
  1761. for (size_t j = sizeof(y->qs) - sizeof(y->qs) % 32; j < sizeof(y->qs); j += 16) {
  1762. for (size_t m = 0; m < 16; ++m) {
  1763. uint8_t q = 0;
  1764. for (size_t n = 0; n < 5; ++n) {
  1765. int xi = lroundf(x[m + n*16] * id) + 1; // -1, 0, 1 -> 0, 1, 2
  1766. q *= 3;
  1767. q += xi;
  1768. }
  1769. // ceiling division (243 == pow(3, 5))
  1770. q = ((uint16_t)q * 256 + (243 - 1)) / 243;
  1771. y[i].qs[j + m] = q;
  1772. }
  1773. x += 5*16;
  1774. }
  1775. // 4 elements per byte
  1776. for (size_t j = 0; j < sizeof(y->qh); ++j) {
  1777. uint8_t q = 0;
  1778. for (size_t m = 0; m < 4; ++m) {
  1779. // -1, 0, 1 -> 0, 1, 2
  1780. int xi = lroundf(x[j + m*sizeof(y->qh)] * id) + 1;
  1781. q *= 3;
  1782. q += xi;
  1783. }
  1784. // shift the first value to the most significant trit
  1785. q *= 3;
  1786. // ceiling division (243 == pow(3, 5))
  1787. q = ((uint16_t)q * 256 + (243 - 1)) / 243;
  1788. y[i].qh[j] = q;
  1789. }
  1790. x += 4*sizeof(y->qh);
  1791. }
  1792. }
  1793. void quantize_row_tq2_0_ref(const float * GGML_RESTRICT x, block_tq2_0 * GGML_RESTRICT y, int64_t k) {
  1794. assert(k % QK_K == 0);
  1795. const int64_t nb = k / QK_K;
  1796. for (int64_t i = 0; i < nb; i++) {
  1797. float amax = 0.0f; // absolute max
  1798. for (int j = 0; j < QK_K; j++) {
  1799. const float v = x[j];
  1800. amax = MAX(amax, fabsf(v));
  1801. }
  1802. const float d = amax;
  1803. const float id = d ? 1.0f/d : 0.0f;
  1804. y[i].d = GGML_FP32_TO_FP16(d);
  1805. for (size_t j = 0; j < sizeof(y->qs); j += 32) {
  1806. for (size_t m = 0; m < 32; ++m) {
  1807. uint8_t q = 0;
  1808. for (size_t n = 0; n < 4; ++n) {
  1809. // -1, 0, 1 -> 0, 1, 2
  1810. int xi = lroundf(x[m + n*32] * id) + 1;
  1811. q += (xi & 3) << (2*n);
  1812. }
  1813. y[i].qs[j + m] = q;
  1814. }
  1815. x += 4*32;
  1816. }
  1817. }
  1818. }
  1819. size_t quantize_tq1_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1820. (void)quant_weights; // not used
  1821. const size_t row_size = ggml_row_size(GGML_TYPE_TQ1_0, n_per_row);
  1822. quantize_row_tq1_0_ref(src, dst, (int64_t)nrow*n_per_row);
  1823. return nrow * row_size;
  1824. }
  1825. size_t quantize_tq2_0(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1826. (void)quant_weights; // not used
  1827. const size_t row_size = ggml_row_size(GGML_TYPE_TQ2_0, n_per_row);
  1828. quantize_row_tq2_0_ref(src, dst, (int64_t)nrow*n_per_row);
  1829. return nrow * row_size;
  1830. }
  1831. void dequantize_row_tq1_0(const block_tq1_0 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  1832. assert(k % QK_K == 0);
  1833. const int64_t nb = k / QK_K;
  1834. const uint8_t pow3[6] = {1, 3, 9, 27, 81, 243};
  1835. for (int64_t i = 0; i < nb; ++i) {
  1836. const float d = GGML_FP16_TO_FP32(x[i].d);
  1837. for (size_t j = 0; j < sizeof(x->qs) - sizeof(x->qs) % 32; j += 32) {
  1838. for (size_t n = 0; n < 5; ++n) {
  1839. for (size_t m = 0; m < 32; ++m) {
  1840. uint8_t q = x[i].qs[j + m] * pow3[n];
  1841. int16_t xi = ((uint16_t) q * 3) >> 8;
  1842. *y++ = (float) (xi - 1) * d;
  1843. }
  1844. }
  1845. }
  1846. for (size_t j = sizeof(x->qs) - sizeof(x->qs) % 32; j < sizeof(x->qs); j += 16) {
  1847. for (size_t n = 0; n < 5; ++n) {
  1848. for (size_t m = 0; m < 16; ++m) {
  1849. uint8_t q = x[i].qs[j + m] * pow3[n];
  1850. int16_t xi = ((uint16_t) q * 3) >> 8;
  1851. *y++ = (float) (xi - 1) * d;
  1852. }
  1853. }
  1854. }
  1855. for (size_t n = 0; n < 4; ++n) {
  1856. for (size_t j = 0; j < sizeof(x->qh); ++j) {
  1857. uint8_t q = x[i].qh[j] * pow3[n];
  1858. int16_t xi = ((uint16_t) q * 3) >> 8;
  1859. *y++ = (float) (xi - 1) * d;
  1860. }
  1861. }
  1862. }
  1863. }
  1864. void dequantize_row_tq2_0(const block_tq2_0 * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  1865. assert(k % QK_K == 0);
  1866. const int64_t nb = k / QK_K;
  1867. for (int64_t i = 0; i < nb; ++i) {
  1868. const float d = GGML_FP16_TO_FP32(x[i].d);
  1869. for (size_t j = 0; j < sizeof(x->qs); j += 32) {
  1870. for (size_t l = 0; l < 4; ++l) {
  1871. for (size_t m = 0; m < 32; ++m) {
  1872. int8_t q = (x[i].qs[j + m] >> (l*2)) & 3;
  1873. *y++ = (float) (q - 1) * d;
  1874. }
  1875. }
  1876. }
  1877. }
  1878. }
  1879. // ====================== "True" 2-bit (de)-quantization
  1880. void dequantize_row_iq2_xxs(const block_iq2_xxs * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  1881. assert(k % QK_K == 0);
  1882. const int64_t nb = k / QK_K;
  1883. uint32_t aux32[2];
  1884. const uint8_t * aux8 = (const uint8_t *)aux32;
  1885. for (int i = 0; i < nb; i++) {
  1886. const float d = GGML_FP16_TO_FP32(x[i].d);
  1887. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  1888. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  1889. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  1890. for (int l = 0; l < 4; ++l) {
  1891. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  1892. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  1893. for (int j = 0; j < 8; ++j) {
  1894. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1895. }
  1896. y += 8;
  1897. }
  1898. }
  1899. }
  1900. }
  1901. // ====================== 2.3125 bpw (de)-quantization
  1902. void dequantize_row_iq2_xs(const block_iq2_xs * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  1903. assert(k % QK_K == 0);
  1904. const int64_t nb = k / QK_K;
  1905. float db[2];
  1906. for (int i = 0; i < nb; i++) {
  1907. const float d = GGML_FP16_TO_FP32(x[i].d);
  1908. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  1909. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  1910. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  1911. for (int l = 0; l < 4; ++l) {
  1912. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  1913. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  1914. for (int j = 0; j < 8; ++j) {
  1915. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  1916. }
  1917. y += 8;
  1918. }
  1919. }
  1920. }
  1921. }
  1922. // ====================== 2.5625 bpw (de)-quantization
  1923. void dequantize_row_iq2_s(const block_iq2_s * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  1924. assert(k % QK_K == 0);
  1925. const int64_t nb = k / QK_K;
  1926. float db[2];
  1927. for (int i = 0; i < nb; i++) {
  1928. const float d = GGML_FP16_TO_FP32(x[i].d);
  1929. const uint8_t * qs = x[i].qs;
  1930. const uint8_t * qh = x[i].qh;
  1931. const uint8_t * signs = qs + QK_K/8;
  1932. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  1933. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  1934. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  1935. for (int l = 0; l < 4; ++l) {
  1936. const float dl = db[l/2];
  1937. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  1938. for (int j = 0; j < 8; ++j) {
  1939. y[j] = dl * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1.f : 1.f);
  1940. }
  1941. y += 8;
  1942. }
  1943. qs += 4;
  1944. signs += 4;
  1945. }
  1946. }
  1947. }
  1948. // ====================== 3.0625 bpw (de)-quantization
  1949. void dequantize_row_iq3_xxs(const block_iq3_xxs * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  1950. assert(k % QK_K == 0);
  1951. const int64_t nb = k / QK_K;
  1952. uint32_t aux32;
  1953. for (int i = 0; i < nb; i++) {
  1954. const float d = GGML_FP16_TO_FP32(x[i].d);
  1955. const uint8_t * qs = x[i].qs;
  1956. const uint8_t * scales_and_signs = qs + QK_K/4;
  1957. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  1958. memcpy(&aux32, scales_and_signs + 4*ib32, sizeof(uint32_t));
  1959. const float db = d * (0.5f + (aux32 >> 28)) * 0.5f;
  1960. for (int l = 0; l < 4; ++l) {
  1961. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  1962. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + qs[2*l+0]);
  1963. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + qs[2*l+1]);
  1964. for (int j = 0; j < 4; ++j) {
  1965. y[j+0] = db * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  1966. y[j+4] = db * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  1967. }
  1968. y += 8;
  1969. }
  1970. qs += 8;
  1971. }
  1972. }
  1973. }
  1974. // ====================== 3.3125 bpw (de)-quantization
  1975. void dequantize_row_iq3_s(const block_iq3_s * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  1976. assert(k % QK_K == 0);
  1977. const int64_t nb = k / QK_K;
  1978. for (int i = 0; i < nb; i++) {
  1979. const float d = GGML_FP16_TO_FP32(x[i].d);
  1980. const uint8_t * qs = x[i].qs;
  1981. const uint8_t * qh = x[i].qh;
  1982. const uint8_t * signs = x[i].signs;
  1983. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  1984. const float db1 = d * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  1985. const float db2 = d * (1 + 2*(x[i].scales[ib32/2] >> 4));
  1986. for (int l = 0; l < 4; ++l) {
  1987. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[0] << (8-2*l)) & 256)));
  1988. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[0] << (7-2*l)) & 256)));
  1989. for (int j = 0; j < 4; ++j) {
  1990. y[j+0] = db1 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  1991. y[j+4] = db1 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  1992. }
  1993. y += 8;
  1994. }
  1995. qs += 8;
  1996. signs += 4;
  1997. for (int l = 0; l < 4; ++l) {
  1998. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[1] << (8-2*l)) & 256)));
  1999. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[1] << (7-2*l)) & 256)));
  2000. for (int j = 0; j < 4; ++j) {
  2001. y[j+0] = db2 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2002. y[j+4] = db2 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2003. }
  2004. y += 8;
  2005. }
  2006. qh += 2;
  2007. qs += 8;
  2008. signs += 4;
  2009. }
  2010. }
  2011. }
  2012. // ====================== 1.5625 bpw (de)-quantization
  2013. void dequantize_row_iq1_s(const block_iq1_s * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  2014. assert(k % QK_K == 0);
  2015. const int64_t nb = k / QK_K;
  2016. for (int i = 0; i < nb; i++) {
  2017. const float d = GGML_FP16_TO_FP32(x[i].d);
  2018. const uint8_t * qs = x[i].qs;
  2019. const uint16_t * qh = x[i].qh;
  2020. for (int ib = 0; ib < QK_K/32; ++ib) {
  2021. const float dl = d * (2*((qh[ib] >> 12) & 7) + 1);
  2022. const float delta = qh[ib] & 0x8000 ? -IQ1S_DELTA : IQ1S_DELTA;
  2023. for (int l = 0; l < 4; ++l) {
  2024. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  2025. for (int j = 0; j < 8; ++j) {
  2026. y[j] = dl * (grid[j] + delta);
  2027. }
  2028. y += 8;
  2029. }
  2030. qs += 4;
  2031. }
  2032. }
  2033. }
  2034. void dequantize_row_iq1_m(const block_iq1_m * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  2035. assert(k % QK_K == 0);
  2036. const int64_t nb = k / QK_K;
  2037. float delta[4];
  2038. uint16_t idx[4];
  2039. iq1m_scale_t scale;
  2040. for (int i = 0; i < nb; i++) {
  2041. const uint16_t * sc = (const uint16_t *)x[i].scales;
  2042. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  2043. const float d = GGML_FP16_TO_FP32(scale.f16);
  2044. const uint8_t * qs = x[i].qs;
  2045. const uint8_t * qh = x[i].qh;
  2046. for (int ib = 0; ib < QK_K/32; ++ib) {
  2047. const float dl1 = d * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1);
  2048. const float dl2 = d * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1);
  2049. idx[0] = qs[0] | ((qh[0] << 8) & 0x700);
  2050. idx[1] = qs[1] | ((qh[0] << 4) & 0x700);
  2051. idx[2] = qs[2] | ((qh[1] << 8) & 0x700);
  2052. idx[3] = qs[3] | ((qh[1] << 4) & 0x700);
  2053. delta[0] = qh[0] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2054. delta[1] = qh[0] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2055. delta[2] = qh[1] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2056. delta[3] = qh[1] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2057. for (int l = 0; l < 2; ++l) {
  2058. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2059. for (int j = 0; j < 8; ++j) {
  2060. y[j] = dl1 * (grid[j] + delta[l]);
  2061. }
  2062. y += 8;
  2063. }
  2064. for (int l = 2; l < 4; ++l) {
  2065. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2066. for (int j = 0; j < 8; ++j) {
  2067. y[j] = dl2 * (grid[j] + delta[l]);
  2068. }
  2069. y += 8;
  2070. }
  2071. qs += 4;
  2072. qh += 2;
  2073. }
  2074. }
  2075. }
  2076. void dequantize_row_iq4_nl(const block_iq4_nl * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  2077. assert(k % QK4_NL == 0);
  2078. const int64_t nb = k / QK4_NL;
  2079. for (int i = 0; i < nb; i++) {
  2080. const uint8_t * qs = x[i].qs;
  2081. const float d = GGML_FP16_TO_FP32(x[i].d);
  2082. for (int j = 0; j < QK4_NL/2; ++j) {
  2083. y[j+ 0] = d * kvalues_iq4nl[qs[j] & 0xf];
  2084. y[j+QK4_NL/2] = d * kvalues_iq4nl[qs[j] >> 4];
  2085. }
  2086. y += QK4_NL;
  2087. qs += QK4_NL/2;
  2088. }
  2089. }
  2090. void dequantize_row_iq4_xs(const block_iq4_xs * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  2091. assert(k % QK_K == 0);
  2092. const int64_t nb = k / QK_K;
  2093. for (int i = 0; i < nb; i++) {
  2094. const uint8_t * qs = x[i].qs;
  2095. const float d = GGML_FP16_TO_FP32(x[i].d);
  2096. for (int ib = 0; ib < QK_K/32; ++ib) {
  2097. const int ls = ((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4);
  2098. const float dl = d * (ls - 32);
  2099. for (int j = 0; j < 16; ++j) {
  2100. y[j+ 0] = dl * kvalues_iq4nl[qs[j] & 0xf];
  2101. y[j+16] = dl * kvalues_iq4nl[qs[j] >> 4];
  2102. }
  2103. y += 32;
  2104. qs += 16;
  2105. }
  2106. }
  2107. }
  2108. //===================================== Q8_K ==============================================
  2109. void quantize_row_q8_K_ref(const float * GGML_RESTRICT x, block_q8_K * GGML_RESTRICT y, int64_t k) {
  2110. assert(k % QK_K == 0);
  2111. const int64_t nb = k / QK_K;
  2112. for (int i = 0; i < nb; i++) {
  2113. float max = 0;
  2114. float amax = 0;
  2115. for (int j = 0; j < QK_K; ++j) {
  2116. float ax = fabsf(x[j]);
  2117. if (ax > amax) {
  2118. amax = ax; max = x[j];
  2119. }
  2120. }
  2121. if (!amax) {
  2122. y[i].d = 0;
  2123. memset(y[i].qs, 0, QK_K);
  2124. x += QK_K;
  2125. continue;
  2126. }
  2127. //const float iscale = -128.f/max;
  2128. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  2129. const float iscale = -127.f/max;
  2130. for (int j = 0; j < QK_K; ++j) {
  2131. int v = nearest_int(iscale*x[j]);
  2132. y[i].qs[j] = MIN(127, v);
  2133. }
  2134. for (int j = 0; j < QK_K/16; ++j) {
  2135. int sum = 0;
  2136. for (int ii = 0; ii < 16; ++ii) {
  2137. sum += y[i].qs[j*16 + ii];
  2138. }
  2139. y[i].bsums[j] = sum;
  2140. }
  2141. y[i].d = 1/iscale;
  2142. x += QK_K;
  2143. }
  2144. }
  2145. void dequantize_row_q8_K(const block_q8_K * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k) {
  2146. assert(k % QK_K == 0);
  2147. const int64_t nb = k / QK_K;
  2148. for (int i = 0; i < nb; i++) {
  2149. for (int j = 0; j < QK_K; ++j) {
  2150. *y++ = x[i].d * x[i].qs[j];
  2151. }
  2152. }
  2153. }
  2154. // ================================ IQ2 quantization =============================================
  2155. typedef struct {
  2156. uint64_t * grid;
  2157. int * map;
  2158. uint16_t * neighbours;
  2159. } iq2_entry_t;
  2160. static iq2_entry_t iq2_data[4] = {
  2161. {NULL, NULL, NULL},
  2162. {NULL, NULL, NULL},
  2163. {NULL, NULL, NULL},
  2164. {NULL, NULL, NULL},
  2165. };
  2166. static inline int iq2_data_index(enum ggml_type type) {
  2167. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  2168. return type == GGML_TYPE_IQ2_XXS ? 0 :
  2169. type == GGML_TYPE_IQ2_XS ? 1 :
  2170. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 2 : 3;
  2171. }
  2172. static inline int iq2_grid_size(enum ggml_type type) {
  2173. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  2174. return type == GGML_TYPE_IQ2_XXS ? 256 :
  2175. type == GGML_TYPE_IQ2_XS ? 512 :
  2176. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? NGRID_IQ1S : 1024;
  2177. }
  2178. static int iq2_compare_func(const void * left, const void * right) {
  2179. const int * l = (const int *)left;
  2180. const int * r = (const int *)right;
  2181. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  2182. }
  2183. void iq2xs_init_impl(enum ggml_type type) {
  2184. const int gindex = iq2_data_index(type);
  2185. const int grid_size = iq2_grid_size(type);
  2186. if (iq2_data[gindex].grid) {
  2187. return;
  2188. }
  2189. static const uint16_t kgrid_2bit_256[256] = {
  2190. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  2191. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  2192. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  2193. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  2194. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  2195. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  2196. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  2197. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  2198. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  2199. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  2200. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  2201. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  2202. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  2203. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  2204. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  2205. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  2206. };
  2207. static const uint16_t kgrid_2bit_512[512] = {
  2208. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  2209. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  2210. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  2211. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  2212. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  2213. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  2214. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  2215. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  2216. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  2217. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  2218. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  2219. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  2220. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  2221. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  2222. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  2223. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  2224. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  2225. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  2226. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  2227. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  2228. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  2229. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  2230. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  2231. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  2232. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  2233. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  2234. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  2235. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  2236. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  2237. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  2238. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  2239. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  2240. };
  2241. static const uint16_t kgrid_1bit_2048[NGRID_IQ1S] = {
  2242. 0, 2, 5, 8, 10, 17, 21, 32, 34, 40, 42, 69, 81, 84, 86, 101,
  2243. 128, 130, 136, 138, 149, 160, 162, 168, 170, 260, 261, 273, 276, 278, 281, 282,
  2244. 293, 321, 326, 329, 338, 341, 346, 353, 356, 358, 360, 389, 401, 404, 406, 421,
  2245. 512, 514, 520, 522, 533, 544, 546, 552, 554, 581, 593, 601, 612, 617, 640, 642,
  2246. 648, 650, 657, 661, 665, 672, 674, 680, 682, 1041, 1044, 1046, 1061, 1089, 1097, 1109,
  2247. 1114, 1124, 1125, 1169, 1177, 1189, 1281, 1284, 1285, 1286, 1301, 1304, 1306, 1321, 1344, 1349,
  2248. 1354, 1360, 1361, 1364, 1365, 1366, 1369, 1376, 1378, 1381, 1384, 1386, 1409, 1425, 1429, 1432,
  2249. 1434, 1441, 1444, 1445, 1446, 1449, 1556, 1561, 1601, 1604, 1616, 1618, 1621, 1624, 1632, 1633,
  2250. 1638, 1641, 1669, 1681, 1684, 1689, 2048, 2050, 2056, 2058, 2069, 2080, 2082, 2088, 2090, 2117,
  2251. 2129, 2134, 2149, 2176, 2178, 2184, 2186, 2197, 2208, 2210, 2216, 2218, 2309, 2321, 2324, 2329,
  2252. 2340, 2341, 2369, 2384, 2385, 2389, 2401, 2404, 2409, 2449, 2452, 2454, 2457, 2469, 2560, 2562,
  2253. 2568, 2570, 2581, 2592, 2594, 2600, 2602, 2629, 2641, 2649, 2657, 2661, 2688, 2690, 2693, 2696,
  2254. 2698, 2709, 2720, 2722, 2728, 2730, 4112, 4113, 4116, 4121, 4132, 4133, 4161, 4164, 4176, 4181,
  2255. 4184, 4193, 4196, 4197, 4201, 4241, 4244, 4246, 4257, 4261, 4353, 4356, 4358, 4361, 4368, 4370,
  2256. 4373, 4376, 4385, 4388, 4393, 4421, 4426, 4432, 4433, 4434, 4436, 4437, 4438, 4441, 4448, 4453,
  2257. 4484, 4498, 4501, 4513, 4516, 4625, 4628, 4630, 4645, 4672, 4678, 4681, 4690, 4693, 4696, 4698,
  2258. 4708, 4710, 4741, 4753, 4756, 4758, 4773, 5121, 5126, 5129, 5140, 5141, 5144, 5145, 5153, 5158,
  2259. 5185, 5189, 5190, 5192, 5194, 5201, 5204, 5205, 5206, 5209, 5218, 5221, 5224, 5252, 5257, 5264,
  2260. 5268, 5269, 5272, 5273, 5274, 5281, 5284, 5285, 5289, 5378, 5381, 5386, 5393, 5396, 5397, 5398,
  2261. 5401, 5408, 5410, 5413, 5416, 5418, 5441, 5444, 5445, 5446, 5457, 5458, 5460, 5461, 5462, 5465,
  2262. 5466, 5473, 5476, 5477, 5478, 5481, 5504, 5506, 5508, 5509, 5512, 5514, 5520, 5521, 5524, 5525,
  2263. 5526, 5529, 5530, 5536, 5538, 5541, 5633, 5636, 5637, 5638, 5653, 5654, 5656, 5658, 5665, 5670,
  2264. 5696, 5698, 5700, 5701, 5704, 5706, 5713, 5717, 5718, 5720, 5721, 5729, 5732, 5733, 5736, 5737,
  2265. 5738, 5766, 5770, 5778, 5781, 5796, 5801, 6161, 6166, 6181, 6209, 6212, 6214, 6217, 6224, 6229,
  2266. 6232, 6234, 6240, 6241, 6244, 6246, 6249, 6277, 6289, 6292, 6309, 6416, 6418, 6421, 6426, 6433,
  2267. 6437, 6466, 6468, 6469, 6472, 6481, 6484, 6485, 6486, 6489, 6490, 6496, 6501, 6506, 6537, 6545,
  2268. 6546, 6549, 6552, 6561, 6566, 6569, 6665, 6678, 6692, 6694, 6724, 6726, 6729, 6736, 6738, 6741,
  2269. 6744, 6753, 6758, 6761, 6789, 6801, 6806, 6810, 8192, 8194, 8200, 8202, 8213, 8224, 8226, 8229,
  2270. 8232, 8234, 8261, 8273, 8281, 8289, 8293, 8320, 8322, 8328, 8330, 8341, 8352, 8354, 8357, 8360,
  2271. 8362, 8453, 8465, 8468, 8473, 8485, 8514, 8516, 8521, 8533, 8536, 8538, 8545, 8548, 8549, 8550,
  2272. 8581, 8592, 8598, 8601, 8613, 8705, 8712, 8714, 8721, 8725, 8736, 8738, 8744, 8746, 8773, 8785,
  2273. 8790, 8793, 8805, 8833, 8840, 8842, 8849, 8853, 8864, 8866, 8872, 8874, 9221, 9236, 9238, 9241,
  2274. 9253, 9284, 9285, 9286, 9289, 9298, 9301, 9304, 9306, 9318, 9349, 9361, 9364, 9369, 9377, 9381,
  2275. 9481, 9493, 9505, 9513, 9536, 9541, 9544, 9553, 9556, 9557, 9561, 9570, 9573, 9576, 9609, 9616,
  2276. 9620, 9621, 9624, 9626, 9633, 9636, 9638, 9641, 9733, 9744, 9746, 9753, 9765, 9793, 9801, 9813,
  2277. 9824, 9825, 9833, 9860, 9862, 9872, 9882, 10240, 10242, 10248, 10250, 10261, 10272, 10274, 10280, 10282,
  2278. 10309, 10321, 10324, 10341, 10368, 10370, 10376, 10378, 10400, 10402, 10408, 10410, 10505, 10513, 10516, 10521,
  2279. 10533, 10566, 10569, 10578, 10581, 10593, 10596, 10598, 10601, 10629, 10640, 10646, 10649, 10660, 10661, 10752,
  2280. 10754, 10760, 10762, 10784, 10786, 10792, 10794, 10821, 10833, 10838, 10841, 10853, 10880, 10882, 10888, 10890,
  2281. 10901, 10912, 10914, 10920, 10922, 16389, 16401, 16406, 16421, 16457, 16466, 16469, 16472, 16474, 16481, 16484,
  2282. 16486, 16532, 16537, 16545, 16550, 16640, 16641, 16644, 16646, 16649, 16658, 16661, 16662, 16664, 16666, 16673,
  2283. 16678, 16681, 16709, 16712, 16714, 16721, 16724, 16725, 16726, 16729, 16730, 16741, 16744, 16746, 16769, 16772,
  2284. 16774, 16784, 16786, 16789, 16800, 16801, 16802, 16901, 16913, 16916, 16918, 16933, 16961, 16978, 16981, 16986,
  2285. 16996, 17001, 17033, 17044, 17061, 17409, 17429, 17433, 17449, 17477, 17480, 17482, 17489, 17492, 17493, 17494,
  2286. 17505, 17506, 17509, 17512, 17514, 17537, 17542, 17545, 17552, 17554, 17557, 17568, 17569, 17577, 17665, 17666,
  2287. 17669, 17674, 17681, 17684, 17685, 17686, 17689, 17696, 17701, 17706, 17729, 17732, 17733, 17734, 17737, 17744,
  2288. 17745, 17748, 17749, 17750, 17752, 17753, 17761, 17764, 17765, 17766, 17769, 17794, 17796, 17797, 17800, 17809,
  2289. 17812, 17813, 17814, 17817, 17818, 17829, 17832, 17834, 17921, 17925, 17929, 17940, 17941, 17944, 17946, 17953,
  2290. 17956, 17961, 17984, 17986, 17989, 17992, 18000, 18001, 18002, 18005, 18006, 18009, 18018, 18021, 18024, 18049,
  2291. 18053, 18058, 18068, 18069, 18081, 18084, 18086, 18437, 18449, 18453, 18458, 18469, 18498, 18505, 18512, 18517,
  2292. 18520, 18529, 18532, 18534, 18537, 18565, 18577, 18580, 18582, 18585, 18597, 18689, 18693, 18694, 18698, 18704,
  2293. 18708, 18709, 18712, 18721, 18724, 18726, 18752, 18757, 18762, 18769, 18770, 18772, 18773, 18774, 18777, 18784,
  2294. 18786, 18789, 18790, 18794, 18822, 18825, 18834, 18837, 18838, 18840, 18849, 18852, 18854, 18857, 18966, 19012,
  2295. 19014, 19017, 19029, 19032, 19034, 19044, 19049, 19092, 19109, 20481, 20484, 20485, 20486, 20489, 20498, 20501,
  2296. 20506, 20513, 20516, 20521, 20544, 20549, 20552, 20561, 20564, 20565, 20566, 20569, 20581, 20584, 20614, 20617,
  2297. 20629, 20632, 20640, 20641, 20646, 20649, 20741, 20744, 20745, 20746, 20753, 20756, 20757, 20758, 20760, 20761,
  2298. 20768, 20773, 20774, 20776, 20778, 20801, 20804, 20805, 20806, 20809, 20816, 20817, 20818, 20820, 20821, 20822,
  2299. 20824, 20825, 20826, 20833, 20836, 20837, 20838, 20841, 20866, 20869, 20881, 20884, 20885, 20886, 20889, 20896,
  2300. 20901, 20906, 20993, 20998, 21010, 21013, 21018, 21025, 21028, 21058, 21061, 21066, 21073, 21076, 21077, 21078,
  2301. 21081, 21090, 21093, 21125, 21136, 21138, 21141, 21145, 21146, 21156, 21508, 21509, 21521, 21524, 21525, 21526,
  2302. 21528, 21529, 21537, 21541, 21544, 21546, 21569, 21572, 21573, 21574, 21577, 21578, 21584, 21585, 21588, 21589,
  2303. 21590, 21592, 21593, 21594, 21601, 21602, 21604, 21605, 21606, 21609, 21632, 21640, 21642, 21649, 21652, 21653,
  2304. 21654, 21657, 21665, 21668, 21669, 21674, 21761, 21762, 21764, 21765, 21766, 21769, 21776, 21777, 21778, 21780,
  2305. 21781, 21782, 21785, 21786, 21793, 21796, 21797, 21798, 21801, 21824, 21825, 21826, 21828, 21829, 21830, 21832,
  2306. 21833, 21840, 21841, 21842, 21844, 21845, 21846, 21848, 21849, 21850, 21856, 21857, 21860, 21861, 21862, 21864,
  2307. 21865, 21866, 21889, 21892, 21893, 21897, 21898, 21904, 21905, 21908, 21909, 21910, 21912, 21913, 21921, 21924,
  2308. 21925, 21926, 21929, 22016, 22017, 22018, 22020, 22022, 22024, 22025, 22033, 22036, 22037, 22040, 22041, 22048,
  2309. 22049, 22050, 22052, 22053, 22054, 22056, 22057, 22081, 22085, 22086, 22088, 22089, 22090, 22096, 22097, 22098,
  2310. 22100, 22101, 22102, 22104, 22105, 22106, 22113, 22116, 22117, 22121, 22146, 22149, 22150, 22152, 22153, 22154,
  2311. 22161, 22165, 22170, 22178, 22181, 22182, 22184, 22185, 22532, 22533, 22534, 22537, 22544, 22549, 22552, 22561,
  2312. 22570, 22597, 22600, 22602, 22609, 22612, 22613, 22614, 22616, 22617, 22624, 22626, 22628, 22629, 22658, 22665,
  2313. 22672, 22674, 22677, 22680, 22689, 22697, 22785, 22786, 22789, 22794, 22801, 22804, 22805, 22806, 22809, 22821,
  2314. 22849, 22852, 22853, 22854, 22857, 22864, 22865, 22866, 22868, 22869, 22870, 22872, 22873, 22874, 22881, 22884,
  2315. 22885, 22886, 22889, 22913, 22917, 22921, 22929, 22932, 22933, 22934, 22936, 22937, 22949, 23044, 23048, 23061,
  2316. 23066, 23072, 23077, 23078, 23081, 23109, 23112, 23113, 23121, 23125, 23126, 23128, 23129, 23138, 23141, 23144,
  2317. 23146, 23169, 23178, 23186, 23189, 23190, 23192, 23194, 23201, 24581, 24596, 24598, 24601, 24613, 24644, 24656,
  2318. 24661, 24662, 24664, 24666, 24673, 24676, 24678, 24681, 24705, 24726, 24741, 24833, 24836, 24838, 24841, 24850,
  2319. 24853, 24865, 24866, 24870, 24873, 24901, 24905, 24913, 24917, 24918, 24921, 24933, 24934, 24938, 24964, 24970,
  2320. 24978, 24981, 24993, 24998, 25001, 25105, 25110, 25113, 25152, 25153, 25158, 25173, 25174, 25176, 25184, 25221,
  2321. 25233, 25238, 25253, 25617, 25618, 25621, 25622, 25626, 25633, 25638, 25641, 25664, 25666, 25669, 25672, 25674,
  2322. 25681, 25684, 25685, 25686, 25689, 25690, 25696, 25698, 25701, 25732, 25733, 25737, 25744, 25746, 25748, 25749,
  2323. 25750, 25752, 25754, 25761, 25764, 25769, 25861, 25864, 25866, 25873, 25877, 25878, 25881, 25924, 25925, 25926,
  2324. 25929, 25936, 25937, 25940, 25941, 25942, 25945, 25953, 25956, 25957, 25958, 25961, 25990, 25993, 25994, 26001,
  2325. 26005, 26006, 26009, 26010, 26018, 26021, 26022, 26024, 26114, 26121, 26133, 26144, 26150, 26152, 26153, 26176,
  2326. 26181, 26184, 26186, 26193, 26196, 26197, 26198, 26200, 26202, 26208, 26213, 26216, 26240, 26242, 26245, 26250,
  2327. 26260, 26262, 26264, 26265, 26272, 26276, 26278, 26282, 26646, 26649, 26661, 26689, 26706, 26709, 26714, 26721,
  2328. 26729, 26757, 26769, 26776, 26790, 26881, 26884, 26896, 26901, 26913, 26916, 26918, 26921, 26944, 26945, 26949,
  2329. 26950, 26952, 26961, 26964, 26965, 26966, 26969, 26976, 26981, 26986, 27010, 27012, 27018, 27029, 27041, 27044,
  2330. 27045, 27049, 27153, 27158, 27160, 27201, 27204, 27209, 27216, 27221, 27224, 27226, 27236, 27237, 27241, 27270,
  2331. 27284, 27288, 27290, 27302, 32768, 32770, 32776, 32778, 32800, 32802, 32808, 32810, 32837, 32848, 32849, 32852,
  2332. 32854, 32857, 32869, 32896, 32898, 32904, 32906, 32917, 32928, 32930, 32936, 32938, 33029, 33041, 33044, 33046,
  2333. 33049, 33061, 33089, 33092, 33097, 33104, 33106, 33109, 33110, 33112, 33113, 33124, 33126, 33129, 33157, 33161,
  2334. 33172, 33174, 33177, 33189, 33280, 33282, 33288, 33290, 33301, 33312, 33314, 33320, 33322, 33361, 33364, 33369,
  2335. 33381, 33408, 33410, 33416, 33418, 33429, 33440, 33442, 33448, 33450, 33812, 33817, 33857, 33860, 33873, 33877,
  2336. 33882, 33889, 33892, 33897, 33940, 33945, 34049, 34057, 34066, 34069, 34074, 34086, 34089, 34112, 34113, 34117,
  2337. 34120, 34129, 34132, 34133, 34134, 34137, 34138, 34149, 34150, 34152, 34154, 34177, 34180, 34182, 34185, 34192,
  2338. 34194, 34197, 34200, 34214, 34321, 34326, 34329, 34341, 34369, 34372, 34377, 34378, 34384, 34389, 34393, 34394,
  2339. 34401, 34406, 34410, 34437, 34449, 34458, 34468, 34816, 34818, 34824, 34826, 34837, 34848, 34850, 34856, 34858,
  2340. 34881, 34885, 34897, 34900, 34905, 34917, 34921, 34944, 34946, 34952, 34954, 34965, 34976, 34978, 34984, 34986,
  2341. 35077, 35078, 35089, 35092, 35094, 35109, 35137, 35140, 35142, 35145, 35152, 35154, 35157, 35162, 35169, 35172,
  2342. 35205, 35222, 35225, 35237, 35328, 35330, 35336, 35338, 35349, 35360, 35362, 35368, 35370, 35397, 35409, 35412,
  2343. 35414, 35456, 35458, 35464, 35466, 35477, 35488, 35490, 35496, 35498, 36869, 36881, 36886, 36888, 36889, 36901,
  2344. 36929, 36934, 36937, 36949, 36952, 36954, 36969, 36970, 36997, 37009, 37012, 37014, 37017, 37029, 37121, 37124,
  2345. 37126, 37129, 37136, 37141, 37144, 37146, 37153, 37156, 37158, 37161, 37184, 37189, 37200, 37201, 37204, 37205,
  2346. 37206, 37209, 37218, 37221, 37252, 37254, 37266, 37269, 37272, 37281, 37284, 37286, 37289, 37381, 37393, 37396,
  2347. 37401, 37413, 37444, 37446, 37449, 37456, 37458, 37461, 37464, 37478, 37481, 37509, 37524, 37526, 37545, 37889,
  2348. 37892, 37894, 37904, 37909, 37912, 37926, 37952, 37962, 37969, 37972, 37973, 37974, 37976, 37977, 37984, 37985,
  2349. 37986, 37989, 38020, 38022, 38034, 38036, 38037, 38040, 38049, 38057, 38144, 38149, 38152, 38154, 38160, 38161,
  2350. 38164, 38165, 38166, 38169, 38177, 38181, 38185, 38186, 38209, 38212, 38213, 38214, 38217, 38224, 38225, 38226,
  2351. 38228, 38229, 38230, 38232, 38233, 38234, 38241, 38244, 38245, 38246, 38249, 38273, 38277, 38280, 38289, 38290,
  2352. 38292, 38293, 38294, 38297, 38298, 38304, 38306, 38309, 38312, 38314, 38401, 38404, 38416, 38421, 38425, 38432,
  2353. 38438, 38441, 38469, 38472, 38473, 38481, 38482, 38485, 38486, 38489, 38501, 38504, 38530, 38532, 38537, 38538,
  2354. 38546, 38548, 38549, 38564, 38566, 38569, 38917, 38934, 38937, 38949, 38977, 38982, 38992, 38994, 38997, 38998,
  2355. 39002, 39012, 39013, 39045, 39057, 39062, 39065, 39077, 39172, 39174, 39177, 39184, 39186, 39189, 39192, 39194,
  2356. 39200, 39201, 39204, 39206, 39232, 39234, 39237, 39240, 39242, 39249, 39252, 39253, 39254, 39257, 39266, 39269,
  2357. 39270, 39274, 39297, 39300, 39312, 39314, 39317, 39322, 39329, 39334, 39429, 39445, 39461, 39492, 39494, 39497,
  2358. 39504, 39509, 39512, 39521, 39557, 39569, 39572, 39573, 39574, 40960, 40962, 40968, 40970, 40981, 40992, 40994,
  2359. 41000, 41002, 41029, 41041, 41044, 41046, 41049, 41088, 41090, 41096, 41098, 41109, 41120, 41122, 41128, 41130,
  2360. 41221, 41225, 41233, 41236, 41238, 41241, 41242, 41286, 41289, 41297, 41301, 41304, 41306, 41313, 41316, 41349,
  2361. 41360, 41362, 41366, 41369, 41474, 41480, 41482, 41488, 41497, 41506, 41512, 41514, 41541, 41553, 41558, 41561,
  2362. 41573, 41600, 41602, 41608, 41610, 41621, 41632, 41634, 41640, 41642, 42009, 42021, 42049, 42052, 42064, 42068,
  2363. 42069, 42072, 42074, 42081, 42085, 42086, 42088, 42089, 42117, 42246, 42249, 42256, 42258, 42261, 42264, 42278,
  2364. 42281, 42306, 42309, 42321, 42324, 42325, 42326, 42329, 42341, 42346, 42369, 42372, 42373, 42374, 42377, 42386,
  2365. 42389, 42392, 42501, 42513, 42518, 42522, 42529, 42533, 42564, 42566, 42570, 42578, 42581, 42582, 42584, 42592,
  2366. 42594, 42630, 42640, 42645, 42646, 42649, 42657, 42660, 42662, 43008, 43010, 43016, 43018, 43040, 43042, 43048,
  2367. 43050, 43089, 43092, 43094, 43097, 43136, 43138, 43144, 43146, 43157, 43168, 43170, 43176, 43178, 43269, 43284,
  2368. 43289, 43297, 43301, 43329, 43344, 43349, 43354, 43361, 43366, 43369, 43408, 43414, 43520, 43522, 43528, 43530,
  2369. 43552, 43554, 43560, 43562, 43601, 43604, 43606, 43648, 43650, 43656, 43658, 43669, 43680, 43682, 43688, 43690,
  2370. };
  2371. static const uint16_t kgrid_2bit_1024[1024] = {
  2372. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  2373. 73, 80, 82, 85, 88, 97, 100, 102, 105, 128, 130, 133, 136, 145, 148, 160,
  2374. 165, 170, 257, 260, 262, 265, 272, 274, 277, 280, 289, 292, 320, 322, 325, 328,
  2375. 337, 340, 342, 345, 352, 357, 360, 385, 388, 400, 402, 405, 417, 420, 512, 514,
  2376. 517, 520, 529, 532, 544, 554, 577, 580, 582, 585, 592, 597, 640, 645, 650, 660,
  2377. 674, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1062, 1065, 1088, 1090, 1093,
  2378. 1096, 1098, 1105, 1108, 1110, 1113, 1120, 1122, 1125, 1153, 1156, 1158, 1161, 1168, 1173, 1176,
  2379. 1185, 1188, 1280, 1282, 1285, 1288, 1290, 1297, 1300, 1302, 1305, 1312, 1317, 1320, 1345, 1348,
  2380. 1350, 1353, 1360, 1362, 1365, 1368, 1377, 1380, 1408, 1410, 1413, 1416, 1425, 1428, 1440, 1537,
  2381. 1540, 1542, 1545, 1552, 1557, 1600, 1605, 1608, 1617, 1620, 1632, 1665, 1668, 1680, 2048, 2050,
  2382. 2053, 2056, 2065, 2068, 2070, 2073, 2080, 2085, 2090, 2113, 2116, 2118, 2121, 2128, 2130, 2133,
  2383. 2136, 2145, 2148, 2176, 2181, 2196, 2218, 2305, 2308, 2320, 2322, 2325, 2328, 2337, 2368, 2373,
  2384. 2376, 2385, 2388, 2400, 2433, 2448, 2560, 2577, 2580, 2594, 2600, 2602, 2640, 2713, 4097, 4100,
  2385. 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4134, 4160, 4162, 4165, 4168, 4177, 4180, 4182,
  2386. 4185, 4192, 4194, 4197, 4200, 4225, 4228, 4230, 4240, 4245, 4248, 4257, 4260, 4352, 4354, 4357,
  2387. 4360, 4362, 4369, 4372, 4374, 4377, 4384, 4386, 4389, 4392, 4417, 4420, 4422, 4425, 4432, 4434,
  2388. 4437, 4440, 4449, 4452, 4480, 4482, 4485, 4488, 4497, 4500, 4609, 4612, 4617, 4624, 4629, 4641,
  2389. 4644, 4672, 4677, 4689, 4692, 4737, 4740, 4752, 5120, 5122, 5125, 5128, 5137, 5140, 5142, 5145,
  2390. 5152, 5157, 5160, 5185, 5188, 5190, 5193, 5200, 5202, 5205, 5208, 5217, 5220, 5248, 5250, 5253,
  2391. 5256, 5265, 5268, 5280, 5377, 5380, 5382, 5385, 5392, 5394, 5397, 5400, 5409, 5412, 5440, 5442,
  2392. 5445, 5448, 5457, 5460, 5472, 5505, 5508, 5520, 5632, 5637, 5640, 5649, 5652, 5664, 5697, 5700,
  2393. 5712, 5760, 5802, 6145, 6148, 6150, 6153, 6160, 6165, 6168, 6177, 6208, 6210, 6213, 6216, 6225,
  2394. 6228, 6240, 6273, 6276, 6400, 6402, 6405, 6408, 6417, 6420, 6432, 6465, 6468, 6480, 6505, 6562,
  2395. 6660, 6672, 6720, 6742, 8192, 8194, 8197, 8200, 8209, 8212, 8214, 8217, 8224, 8229, 8234, 8257,
  2396. 8260, 8272, 8274, 8277, 8292, 8320, 8330, 8340, 8362, 8449, 8452, 8464, 8466, 8469, 8481, 8512,
  2397. 8514, 8517, 8529, 8532, 8544, 8577, 8580, 8592, 8704, 8714, 8738, 8744, 8746, 8772, 8784, 8840,
  2398. 8842, 8872, 9217, 9220, 9222, 9225, 9232, 9237, 9240, 9249, 9252, 9280, 9282, 9285, 9288, 9297,
  2399. 9300, 9312, 9345, 9348, 9360, 9472, 9477, 9480, 9489, 9492, 9504, 9537, 9540, 9552, 9574, 9600,
  2400. 9729, 9732, 9744, 9792, 9817, 10240, 10245, 10257, 10260, 10305, 10308, 10320, 10378, 10410, 10497, 10500,
  2401. 10512, 10645, 10762, 10786, 10852, 10888, 10890, 16385, 16388, 16390, 16393, 16400, 16402, 16405, 16408, 16410,
  2402. 16417, 16420, 16422, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16470, 16473, 16480, 16482, 16485, 16513,
  2403. 16516, 16528, 16533, 16536, 16545, 16548, 16640, 16642, 16645, 16648, 16657, 16660, 16662, 16665, 16672, 16674,
  2404. 16677, 16705, 16708, 16710, 16713, 16720, 16722, 16725, 16728, 16737, 16740, 16768, 16770, 16773, 16776, 16785,
  2405. 16788, 16800, 16897, 16900, 16912, 16914, 16917, 16920, 16932, 16960, 16965, 16968, 16977, 16980, 16992, 17025,
  2406. 17028, 17408, 17410, 17413, 17416, 17418, 17425, 17428, 17430, 17433, 17440, 17442, 17445, 17448, 17473, 17476,
  2407. 17478, 17481, 17488, 17490, 17493, 17496, 17505, 17508, 17536, 17538, 17541, 17544, 17553, 17556, 17568, 17665,
  2408. 17668, 17670, 17673, 17680, 17682, 17685, 17688, 17697, 17700, 17728, 17730, 17733, 17736, 17745, 17748, 17760,
  2409. 17770, 17793, 17796, 17808, 17920, 17922, 17925, 17928, 17937, 17940, 17952, 17985, 17988, 18000, 18048, 18085,
  2410. 18433, 18436, 18441, 18448, 18450, 18453, 18456, 18465, 18468, 18496, 18498, 18501, 18504, 18513, 18516, 18528,
  2411. 18564, 18576, 18688, 18690, 18693, 18696, 18705, 18708, 18720, 18753, 18756, 18768, 18816, 18838, 18945, 18948,
  2412. 18960, 19008, 20480, 20482, 20485, 20488, 20497, 20500, 20502, 20505, 20512, 20514, 20517, 20520, 20545, 20548,
  2413. 20550, 20553, 20560, 20562, 20565, 20568, 20577, 20580, 20608, 20610, 20613, 20616, 20625, 20628, 20737, 20740,
  2414. 20742, 20745, 20752, 20754, 20757, 20760, 20769, 20772, 20800, 20802, 20805, 20808, 20817, 20820, 20832, 20865,
  2415. 20868, 20880, 20992, 20997, 21000, 21009, 21012, 21024, 21057, 21060, 21072, 21097, 21120, 21505, 21508, 21510,
  2416. 21513, 21520, 21522, 21525, 21528, 21537, 21540, 21568, 21570, 21573, 21576, 21585, 21588, 21600, 21633, 21636,
  2417. 21648, 21760, 21762, 21765, 21768, 21777, 21780, 21792, 21825, 21828, 21840, 21888, 22017, 22020, 22032, 22054,
  2418. 22080, 22528, 22530, 22533, 22536, 22545, 22548, 22560, 22593, 22596, 22608, 22618, 22656, 22785, 22788, 22800,
  2419. 22848, 23040, 23065, 23173, 23208, 24577, 24580, 24582, 24592, 24594, 24597, 24600, 24609, 24612, 24640, 24645,
  2420. 24648, 24657, 24660, 24672, 24708, 24720, 24832, 24834, 24837, 24840, 24849, 24852, 24864, 24897, 24900, 24912,
  2421. 24960, 24985, 25092, 25104, 25152, 25174, 25249, 25600, 25605, 25608, 25617, 25620, 25632, 25665, 25668, 25680,
  2422. 25728, 25857, 25860, 25872, 25920, 25930, 25960, 26002, 26112, 26260, 26625, 26628, 26640, 26725, 26776, 26880,
  2423. 26922, 27202, 27297, 32768, 32770, 32773, 32776, 32785, 32788, 32793, 32800, 32805, 32833, 32836, 32848, 32850,
  2424. 32853, 32856, 32865, 32896, 32901, 32913, 32916, 33025, 33028, 33033, 33040, 33042, 33045, 33048, 33057, 33060,
  2425. 33088, 33090, 33093, 33096, 33105, 33108, 33153, 33156, 33168, 33193, 33280, 33285, 33290, 33297, 33300, 33345,
  2426. 33348, 33360, 33793, 33796, 33798, 33801, 33808, 33810, 33813, 33816, 33825, 33856, 33858, 33861, 33864, 33873,
  2427. 33876, 33888, 33921, 33924, 33936, 34048, 34050, 34053, 34056, 34065, 34068, 34080, 34113, 34116, 34128, 34176,
  2428. 34186, 34305, 34308, 34320, 34345, 34368, 34816, 34821, 34833, 34836, 34881, 34884, 34896, 34978, 35073, 35076,
  2429. 35136, 35173, 35362, 35416, 35418, 35458, 35490, 36865, 36868, 36873, 36880, 36882, 36885, 36888, 36900, 36928,
  2430. 36930, 36933, 36936, 36945, 36948, 36960, 36993, 36996, 37008, 37120, 37125, 37137, 37140, 37185, 37188, 37200,
  2431. 37210, 37377, 37380, 37392, 37440, 37542, 37888, 37890, 37893, 37896, 37905, 37908, 37920, 37953, 37956, 37968,
  2432. 38016, 38038, 38145, 38148, 38160, 38208, 38296, 38305, 38400, 38470, 38500, 38913, 38916, 38928, 38950, 38976,
  2433. 39081, 39168, 39241, 39250, 39568, 40960, 40965, 40970, 40980, 40994, 41002, 41025, 41028, 41040, 41122, 41130,
  2434. 41280, 41317, 41474, 41482, 41506, 41512, 41514, 41602, 41608, 41610, 41640, 41985, 41988, 42000, 42048, 42121,
  2435. 42148, 42240, 42265, 42577, 43018, 43048, 43170, 43348, 43398, 43528, 43530, 43552, 43554, 43560, 43656, 43690,
  2436. };
  2437. const int kmap_size = 43692;
  2438. //const int nwant = type == GGML_TYPE_IQ1_S ? 3 : 2;
  2439. const int nwant = type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 3 : type == GGML_TYPE_IQ2_S ? 1 : 2;
  2440. const uint16_t * kgrid = type == GGML_TYPE_IQ2_XXS ? kgrid_2bit_256 :
  2441. type == GGML_TYPE_IQ2_XS ? kgrid_2bit_512 :
  2442. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? kgrid_1bit_2048 : kgrid_2bit_1024;
  2443. uint64_t * kgrid_q2xs;
  2444. int * kmap_q2xs;
  2445. uint16_t * kneighbors_q2xs;
  2446. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  2447. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  2448. for (int k = 0; k < grid_size; ++k) {
  2449. int8_t * pos = (int8_t *)(the_grid + k);
  2450. for (int i = 0; i < 8; ++i) {
  2451. int l = (kgrid[k] >> 2*i) & 0x3;
  2452. pos[i] = 2*l + 1;
  2453. }
  2454. }
  2455. kgrid_q2xs = the_grid;
  2456. iq2_data[gindex].grid = the_grid;
  2457. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  2458. iq2_data[gindex].map = kmap_q2xs;
  2459. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  2460. uint64_t aux64;
  2461. uint8_t * aux8 = (uint8_t *)&aux64;
  2462. for (int i = 0; i < grid_size; ++i) {
  2463. aux64 = kgrid_q2xs[i];
  2464. uint16_t index = 0;
  2465. for (int k=0; k<8; ++k) {
  2466. uint16_t q = (aux8[k] - 1)/2;
  2467. index |= (q << 2*k);
  2468. }
  2469. kmap_q2xs[index] = i;
  2470. }
  2471. int8_t pos[8];
  2472. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  2473. int num_neighbors = 0, num_not_in_map = 0;
  2474. for (int i = 0; i < kmap_size; ++i) {
  2475. if (kmap_q2xs[i] >= 0) continue;
  2476. ++num_not_in_map;
  2477. for (int k = 0; k < 8; ++k) {
  2478. int l = (i >> 2*k) & 0x3;
  2479. pos[k] = 2*l + 1;
  2480. }
  2481. for (int j = 0; j < grid_size; ++j) {
  2482. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  2483. int d2 = 0;
  2484. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  2485. dist2[2*j+0] = d2;
  2486. dist2[2*j+1] = j;
  2487. }
  2488. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  2489. int n = 0; int d2 = dist2[0];
  2490. int nhave = 1;
  2491. for (int j = 0; j < grid_size; ++j) {
  2492. if (dist2[2*j] > d2) {
  2493. if (nhave == nwant) break;
  2494. d2 = dist2[2*j];
  2495. ++nhave;
  2496. }
  2497. ++n;
  2498. }
  2499. num_neighbors += n;
  2500. }
  2501. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  2502. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  2503. iq2_data[gindex].neighbours = kneighbors_q2xs;
  2504. int counter = 0;
  2505. for (int i = 0; i < kmap_size; ++i) {
  2506. if (kmap_q2xs[i] >= 0) continue;
  2507. for (int k = 0; k < 8; ++k) {
  2508. int l = (i >> 2*k) & 0x3;
  2509. pos[k] = 2*l + 1;
  2510. }
  2511. for (int j = 0; j < grid_size; ++j) {
  2512. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  2513. int d2 = 0;
  2514. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  2515. dist2[2*j+0] = d2;
  2516. dist2[2*j+1] = j;
  2517. }
  2518. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  2519. kmap_q2xs[i] = -(counter + 1);
  2520. int d2 = dist2[0];
  2521. uint16_t * start = &kneighbors_q2xs[counter++];
  2522. int n = 0, nhave = 1;
  2523. for (int j = 0; j < grid_size; ++j) {
  2524. if (dist2[2*j] > d2) {
  2525. if (nhave == nwant) break;
  2526. d2 = dist2[2*j];
  2527. ++nhave;
  2528. }
  2529. kneighbors_q2xs[counter++] = dist2[2*j+1];
  2530. ++n;
  2531. }
  2532. *start = n;
  2533. }
  2534. free(dist2);
  2535. }
  2536. void iq2xs_free_impl(enum ggml_type type) {
  2537. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  2538. const int gindex = iq2_data_index(type);
  2539. if (iq2_data[gindex].grid) {
  2540. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  2541. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  2542. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  2543. }
  2544. }
  2545. static int iq2_find_best_neighbour(const uint16_t * GGML_RESTRICT neighbours, const uint64_t * GGML_RESTRICT grid,
  2546. const float * GGML_RESTRICT xval, const float * GGML_RESTRICT weight, float scale, int8_t * GGML_RESTRICT L) {
  2547. int num_neighbors = neighbours[0];
  2548. GGML_ASSERT(num_neighbors > 0);
  2549. float best_d2 = FLT_MAX;
  2550. int grid_index = -1;
  2551. for (int j = 1; j <= num_neighbors; ++j) {
  2552. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  2553. float d2 = 0;
  2554. for (int i = 0; i < 8; ++i) {
  2555. float q = pg[i];
  2556. float diff = scale*q - xval[i];
  2557. d2 += weight[i]*diff*diff;
  2558. }
  2559. if (d2 < best_d2) {
  2560. best_d2 = d2; grid_index = neighbours[j];
  2561. }
  2562. }
  2563. GGML_ASSERT(grid_index >= 0);
  2564. const int8_t * pg = (const int8_t *)(grid + grid_index);
  2565. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  2566. return grid_index;
  2567. }
  2568. static void quantize_row_iq2_xxs_impl(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t n, const float * GGML_RESTRICT quant_weights) {
  2569. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XXS);
  2570. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  2571. const int * kmap_q2xs = iq2_data[gindex].map;
  2572. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  2573. GGML_ASSERT(quant_weights && "missing quantization weights");
  2574. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  2575. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  2576. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  2577. GGML_ASSERT(n%QK_K == 0);
  2578. const int kMaxQ = 3;
  2579. const int64_t nbl = n/QK_K;
  2580. block_iq2_xxs * y = vy;
  2581. float scales[QK_K/32];
  2582. float weight[32];
  2583. float xval[32];
  2584. int8_t L[32];
  2585. int8_t Laux[32];
  2586. float waux[32];
  2587. uint8_t block_signs[4];
  2588. uint32_t q2[2*(QK_K/32)];
  2589. for (int ibl = 0; ibl < nbl; ++ibl) {
  2590. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  2591. memset(q2, 0, QK_K/4);
  2592. float max_scale = 0;
  2593. const float * xbl = x + QK_K*ibl;
  2594. float sumx2 = 0;
  2595. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  2596. float sigma2 = sumx2/QK_K;
  2597. for (int ib = 0; ib < QK_K/32; ++ib) {
  2598. const float * xb = xbl + 32*ib;
  2599. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  2600. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  2601. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  2602. for (int k = 0; k < 4; ++k) {
  2603. int nflip = 0;
  2604. uint8_t s = 0;
  2605. for (int i = 0; i < 8; ++i) {
  2606. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  2607. else {
  2608. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  2609. }
  2610. }
  2611. if (nflip%2) {
  2612. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  2613. for (int i = 1; i < 8; ++i) {
  2614. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  2615. if (ax < min) {
  2616. min = ax; imin = i;
  2617. }
  2618. }
  2619. xval[8*k+imin] = -xval[8*k+imin];
  2620. s ^= (1 << imin);
  2621. }
  2622. block_signs[k] = s & 127;
  2623. }
  2624. float max = xval[0];
  2625. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  2626. if (max < GROUP_MAX_EPS) {
  2627. scales[ib] = 0;
  2628. memset(L, 0, 32);
  2629. continue;
  2630. }
  2631. float scale = make_qp_quants(32, kMaxQ+1, xval, (uint8_t*)L, weight);
  2632. float eff_max = scale*kMaxQ;
  2633. float best = 0;
  2634. for (int is = -6; is <= 6; ++is) {
  2635. float id = (2*kMaxQ-1+is*0.1f)/eff_max;
  2636. float this_scale = 1/id;
  2637. for (int k = 0; k < 4; ++k) {
  2638. for (int i = 0; i < 8; ++i) {
  2639. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  2640. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  2641. }
  2642. uint16_t u = 0;
  2643. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  2644. int grid_index = kmap_q2xs[u];
  2645. if (grid_index < 0) {
  2646. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  2647. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  2648. }
  2649. }
  2650. float sumqx = 0, sumq2 = 0;
  2651. for (int i = 0; i < 32; ++i) {
  2652. float w = weight[i];
  2653. float q = 2*Laux[i] + 1;
  2654. sumqx += w*xval[i]*q;
  2655. sumq2 += w*q*q;
  2656. }
  2657. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  2658. scale = sumqx/sumq2; best = scale*sumqx;
  2659. memcpy(L, Laux, 32);
  2660. }
  2661. }
  2662. if (scale > 0) {
  2663. float id = 1/scale;
  2664. for (int k = 0; k < 4; ++k) {
  2665. uint16_t u = 0;
  2666. for (int i = 0; i < 8; ++i) {
  2667. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  2668. l = MAX(0, MIN(kMaxQ-1, l));
  2669. u |= (l << 2*i);
  2670. }
  2671. int grid_index = kmap_q2xs[u];
  2672. if (grid_index < 0) {
  2673. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  2674. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  2675. }
  2676. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  2677. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  2678. }
  2679. float sumqx = 0, sumq2 = 0;
  2680. for (int i = 0; i < 32; ++i) {
  2681. float w = weight[i];
  2682. float q = 2*L[i] + 1;
  2683. sumqx += w*xval[i]*q;
  2684. sumq2 += w*q*q;
  2685. }
  2686. if (sumq2 > 0) scale = sumqx/sumq2;
  2687. }
  2688. if (scale < 0) {
  2689. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  2690. // and correspondingly flip quant signs.
  2691. scale = -scale;
  2692. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  2693. }
  2694. for (int k = 0; k < 4; ++k) {
  2695. uint16_t u = 0;
  2696. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  2697. int grid_index = kmap_q2xs[u];
  2698. if (grid_index < 0) {
  2699. printf("Oops: found point %u not on grid:", u);
  2700. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  2701. printf("\n");
  2702. GGML_ABORT("fatal error");
  2703. }
  2704. q2[2*ib+0] |= ((uint32_t) grid_index << 8*k);
  2705. q2[2*ib+1] |= (block_signs[k] << 7*k);
  2706. }
  2707. GGML_ASSERT(scale >= 0);
  2708. scales[ib] = scale;
  2709. max_scale = MAX(max_scale, scale);
  2710. }
  2711. if (!max_scale) {
  2712. memset(y[ibl].qs, 0, QK_K/4);
  2713. continue;
  2714. }
  2715. float d = max_scale/31;
  2716. y[ibl].d = GGML_FP32_TO_FP16(d);
  2717. float id = 1/d;
  2718. for (int ib = 0; ib < QK_K/32; ++ib) {
  2719. int l = nearest_int(0.5f*(id*scales[ib]-1));
  2720. l = MAX(0, MIN(15, l));
  2721. q2[2*ib+1] |= ((uint32_t)l << 28);
  2722. }
  2723. memcpy(y[ibl].qs, q2, QK_K/4);
  2724. }
  2725. }
  2726. static void quantize_row_iq2_xs_impl(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t n, const float * GGML_RESTRICT quant_weights) {
  2727. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XS);
  2728. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  2729. const int * kmap_q2xs = iq2_data[gindex].map;
  2730. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  2731. GGML_ASSERT(quant_weights && "missing quantization weights");
  2732. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  2733. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  2734. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  2735. GGML_ASSERT(n%QK_K == 0);
  2736. const int kMaxQ = 3;
  2737. const int64_t nbl = n/QK_K;
  2738. block_iq2_xs * y = vy;
  2739. float scales[QK_K/16];
  2740. float weight[16];
  2741. float xval[16];
  2742. int8_t L[16];
  2743. int8_t Laux[16];
  2744. float waux[16];
  2745. bool is_on_grid[2];
  2746. bool is_on_grid_aux[2];
  2747. uint8_t block_signs[2];
  2748. uint16_t q2[2*(QK_K/16)];
  2749. for (int ibl = 0; ibl < nbl; ++ibl) {
  2750. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  2751. memset(q2, 0, QK_K/4);
  2752. memset(y[ibl].scales, 0, QK_K/32);
  2753. float max_scale = 0;
  2754. const float * xbl = x + QK_K*ibl;
  2755. float sumx2 = 0;
  2756. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  2757. float sigma2 = sumx2/QK_K;
  2758. for (int ib = 0; ib < QK_K/16; ++ib) {
  2759. const float * xb = xbl + 16*ib;
  2760. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  2761. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  2762. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  2763. for (int k = 0; k < 2; ++k) {
  2764. int nflip = 0;
  2765. uint8_t s = 0;
  2766. for (int i = 0; i < 8; ++i) {
  2767. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  2768. else {
  2769. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  2770. }
  2771. }
  2772. if (nflip%2) {
  2773. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  2774. for (int i = 1; i < 8; ++i) {
  2775. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  2776. if (ax < min) {
  2777. min = ax; imin = i;
  2778. }
  2779. }
  2780. xval[8*k+imin] = -xval[8*k+imin];
  2781. s ^= (1 << imin);
  2782. }
  2783. block_signs[k] = s & 127;
  2784. }
  2785. float max = xval[0];
  2786. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  2787. if (max < GROUP_MAX_EPS) {
  2788. scales[ib] = 0;
  2789. memset(L, 0, 16);
  2790. continue;
  2791. }
  2792. float best = 0;
  2793. float scale = max/(2*kMaxQ-1);
  2794. is_on_grid[0] = is_on_grid[1] = true;
  2795. for (int is = -9; is <= 9; ++is) {
  2796. float id = (2*kMaxQ-1+is*0.1f)/max;
  2797. float this_scale = 1/id;
  2798. for (int k = 0; k < 2; ++k) {
  2799. for (int i = 0; i < 8; ++i) {
  2800. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  2801. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  2802. }
  2803. uint16_t u = 0;
  2804. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  2805. int grid_index = kmap_q2xs[u];
  2806. is_on_grid_aux[k] = true;
  2807. if (grid_index < 0) {
  2808. is_on_grid_aux[k] = false;
  2809. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  2810. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  2811. }
  2812. }
  2813. float sumqx = 0, sumq2 = 0;
  2814. for (int i = 0; i < 16; ++i) {
  2815. float w = weight[i];
  2816. float q = 2*Laux[i] + 1;
  2817. sumqx += w*xval[i]*q;
  2818. sumq2 += w*q*q;
  2819. }
  2820. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  2821. scale = sumqx/sumq2; best = scale*sumqx;
  2822. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  2823. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  2824. }
  2825. }
  2826. int n_not_ongrid = 0;
  2827. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  2828. if (n_not_ongrid > 0 && scale > 0) {
  2829. float id = 1/scale;
  2830. for (int k = 0; k < 2; ++k) {
  2831. if (is_on_grid[k]) continue;
  2832. uint16_t u = 0;
  2833. for (int i = 0; i < 8; ++i) {
  2834. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  2835. l = MAX(0, MIN(kMaxQ-1, l));
  2836. u |= (l << 2*i);
  2837. L[8*k + i] = l;
  2838. }
  2839. int grid_index = kmap_q2xs[u];
  2840. if (grid_index < 0) {
  2841. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  2842. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  2843. }
  2844. }
  2845. float sumqx = 0, sumq2 = 0;
  2846. for (int i = 0; i < 16; ++i) {
  2847. float w = weight[i];
  2848. float q = 2*L[i] + 1;
  2849. sumqx += w*xval[i]*q;
  2850. sumq2 += w*q*q;
  2851. }
  2852. if (sumq2 > 0) scale = sumqx/sumq2;
  2853. }
  2854. if (scale < 0) {
  2855. scale = -scale;
  2856. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  2857. }
  2858. for (int k = 0; k < 2; ++k) {
  2859. uint16_t u = 0;
  2860. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  2861. int grid_index = kmap_q2xs[u];
  2862. if (grid_index < 0) {
  2863. printf("Oops: found point %u not on grid:", u);
  2864. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  2865. printf("\n");
  2866. GGML_ABORT("fatal error");
  2867. }
  2868. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  2869. }
  2870. GGML_ASSERT(scale >= 0);
  2871. scales[ib] = scale;
  2872. max_scale = MAX(max_scale, scale);
  2873. }
  2874. if (!max_scale) {
  2875. memset(y[ibl].qs, 0, QK_K/4);
  2876. continue;
  2877. }
  2878. float d = max_scale/31;
  2879. y[ibl].d = GGML_FP32_TO_FP16(d);
  2880. float id = 1/d;
  2881. for (int ib = 0; ib < QK_K/16; ++ib) {
  2882. int l = nearest_int(0.5f*(id*scales[ib]-1));
  2883. l = MAX(0, MIN(15, l));
  2884. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  2885. else y[ibl].scales[ib/2] |= (l << 4);
  2886. }
  2887. memcpy(y[ibl].qs, q2, QK_K/4);
  2888. }
  2889. }
  2890. size_t quantize_iq2_xxs(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2891. GGML_ASSERT(n_per_row%QK_K == 0);
  2892. int64_t nblock = n_per_row/QK_K;
  2893. char * qrow = (char *)dst;
  2894. for (int64_t row = 0; row < nrow; ++row) {
  2895. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  2896. src += n_per_row;
  2897. qrow += nblock*sizeof(block_iq2_xxs);
  2898. }
  2899. return nrow * nblock * sizeof(block_iq2_xxs);
  2900. }
  2901. size_t quantize_iq2_xs(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2902. GGML_ASSERT(n_per_row%QK_K == 0);
  2903. int64_t nblock = n_per_row/QK_K;
  2904. char * qrow = (char *)dst;
  2905. for (int64_t row = 0; row < nrow; ++row) {
  2906. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  2907. src += n_per_row;
  2908. qrow += nblock*sizeof(block_iq2_xs);
  2909. }
  2910. return nrow * nblock * sizeof(block_iq2_xs);
  2911. }
  2912. //
  2913. // ============================================= 3-bit using D4 lattice
  2914. //
  2915. typedef struct {
  2916. uint32_t * grid;
  2917. int * map;
  2918. uint16_t * neighbours;
  2919. } iq3_entry_t;
  2920. static iq3_entry_t iq3_data[2] = {
  2921. {NULL, NULL, NULL},
  2922. {NULL, NULL, NULL},
  2923. };
  2924. static inline int iq3_data_index(int grid_size) {
  2925. (void)grid_size;
  2926. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  2927. return grid_size == 256 ? 0 : 1;
  2928. }
  2929. static int iq3_compare_func(const void * left, const void * right) {
  2930. const int * l = (const int *)left;
  2931. const int * r = (const int *)right;
  2932. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  2933. }
  2934. void iq3xs_init_impl(int grid_size) {
  2935. const int gindex = iq3_data_index(grid_size);
  2936. if (iq3_data[gindex].grid) {
  2937. return;
  2938. }
  2939. static const uint16_t kgrid_256[256] = {
  2940. 0, 2, 4, 9, 11, 15, 16, 18, 25, 34, 59, 61, 65, 67, 72, 74,
  2941. 81, 85, 88, 90, 97, 108, 120, 128, 130, 132, 137, 144, 146, 153, 155, 159,
  2942. 169, 175, 189, 193, 199, 200, 202, 213, 248, 267, 287, 292, 303, 315, 317, 321,
  2943. 327, 346, 362, 413, 436, 456, 460, 462, 483, 497, 513, 515, 520, 522, 529, 531,
  2944. 536, 538, 540, 551, 552, 576, 578, 585, 592, 594, 641, 643, 648, 650, 657, 664,
  2945. 698, 704, 706, 720, 729, 742, 758, 769, 773, 808, 848, 852, 870, 889, 901, 978,
  2946. 992, 1024, 1026, 1033, 1035, 1040, 1042, 1046, 1049, 1058, 1089, 1091, 1093, 1096, 1098, 1105,
  2947. 1112, 1139, 1143, 1144, 1152, 1154, 1161, 1167, 1168, 1170, 1183, 1184, 1197, 1217, 1224, 1228,
  2948. 1272, 1276, 1309, 1323, 1347, 1367, 1377, 1404, 1473, 1475, 1486, 1509, 1537, 1544, 1546, 1553,
  2949. 1555, 1576, 1589, 1594, 1600, 1602, 1616, 1625, 1636, 1638, 1665, 1667, 1672, 1685, 1706, 1722,
  2950. 1737, 1755, 1816, 1831, 1850, 1856, 1862, 1874, 1901, 1932, 1950, 1971, 2011, 2032, 2052, 2063,
  2951. 2077, 2079, 2091, 2095, 2172, 2192, 2207, 2208, 2224, 2230, 2247, 2277, 2308, 2345, 2356, 2389,
  2952. 2403, 2424, 2501, 2504, 2506, 2520, 2570, 2593, 2616, 2624, 2630, 2646, 2669, 2700, 2714, 2746,
  2953. 2754, 2795, 2824, 2835, 2839, 2874, 2882, 2905, 2984, 3028, 3042, 3092, 3108, 3110, 3124, 3153,
  2954. 3185, 3215, 3252, 3288, 3294, 3364, 3397, 3434, 3483, 3523, 3537, 3587, 3589, 3591, 3592, 3610,
  2955. 3626, 3670, 3680, 3722, 3749, 3754, 3776, 3789, 3803, 3824, 3857, 3873, 3904, 3906, 3924, 3992,
  2956. };
  2957. static const uint16_t kgrid_512[512] = {
  2958. 0, 1, 2, 5, 7, 8, 9, 10, 12, 14, 16, 17, 21, 27, 32, 34,
  2959. 37, 39, 41, 43, 48, 50, 57, 60, 63, 64, 65, 66, 68, 72, 73, 77,
  2960. 80, 83, 87, 89, 93, 100, 113, 117, 122, 128, 129, 133, 135, 136, 139, 142,
  2961. 145, 149, 152, 156, 162, 165, 167, 169, 171, 184, 187, 195, 201, 205, 208, 210,
  2962. 217, 219, 222, 228, 232, 234, 247, 249, 253, 256, 267, 271, 273, 276, 282, 288,
  2963. 291, 297, 312, 322, 324, 336, 338, 342, 347, 353, 357, 359, 374, 379, 390, 393,
  2964. 395, 409, 426, 441, 448, 450, 452, 464, 466, 470, 475, 488, 492, 512, 513, 514,
  2965. 516, 520, 521, 523, 525, 527, 528, 530, 537, 540, 542, 556, 558, 561, 570, 576,
  2966. 577, 579, 582, 584, 588, 593, 600, 603, 609, 616, 618, 632, 638, 640, 650, 653,
  2967. 655, 656, 660, 666, 672, 675, 685, 688, 698, 705, 708, 711, 712, 715, 721, 727,
  2968. 728, 732, 737, 754, 760, 771, 773, 778, 780, 793, 795, 802, 806, 808, 812, 833,
  2969. 840, 843, 849, 856, 858, 873, 912, 916, 919, 932, 934, 961, 963, 968, 970, 977,
  2970. 989, 993, 1010, 1016, 1024, 1025, 1027, 1029, 1031, 1032, 1034, 1036, 1038, 1041, 1043, 1047,
  2971. 1048, 1050, 1057, 1059, 1061, 1064, 1066, 1079, 1080, 1083, 1085, 1088, 1090, 1096, 1099, 1103,
  2972. 1106, 1109, 1113, 1116, 1122, 1129, 1153, 1156, 1159, 1169, 1171, 1176, 1183, 1185, 1195, 1199,
  2973. 1209, 1212, 1216, 1218, 1221, 1225, 1234, 1236, 1241, 1243, 1250, 1256, 1270, 1281, 1287, 1296,
  2974. 1299, 1306, 1309, 1313, 1338, 1341, 1348, 1353, 1362, 1375, 1376, 1387, 1400, 1408, 1410, 1415,
  2975. 1425, 1453, 1457, 1477, 1481, 1494, 1496, 1507, 1512, 1538, 1545, 1547, 1549, 1551, 1554, 1561,
  2976. 1563, 1565, 1570, 1572, 1575, 1577, 1587, 1593, 1601, 1603, 1605, 1612, 1617, 1619, 1632, 1648,
  2977. 1658, 1662, 1664, 1674, 1680, 1690, 1692, 1704, 1729, 1736, 1740, 1745, 1747, 1751, 1752, 1761,
  2978. 1763, 1767, 1773, 1787, 1795, 1801, 1806, 1810, 1817, 1834, 1840, 1844, 1857, 1864, 1866, 1877,
  2979. 1882, 1892, 1902, 1915, 1934, 1953, 1985, 1987, 2000, 2002, 2013, 2048, 2052, 2058, 2064, 2068,
  2980. 2071, 2074, 2081, 2088, 2104, 2114, 2119, 2121, 2123, 2130, 2136, 2141, 2147, 2153, 2157, 2177,
  2981. 2179, 2184, 2189, 2193, 2203, 2208, 2223, 2226, 2232, 2244, 2249, 2251, 2256, 2258, 2265, 2269,
  2982. 2304, 2306, 2324, 2335, 2336, 2361, 2373, 2375, 2385, 2418, 2443, 2460, 2480, 2504, 2509, 2520,
  2983. 2531, 2537, 2562, 2568, 2572, 2578, 2592, 2596, 2599, 2602, 2614, 2620, 2625, 2627, 2629, 2634,
  2984. 2641, 2650, 2682, 2688, 2697, 2707, 2712, 2718, 2731, 2754, 2759, 2760, 2775, 2788, 2793, 2805,
  2985. 2811, 2817, 2820, 2832, 2842, 2854, 2890, 2902, 2921, 2923, 2978, 3010, 3012, 3026, 3081, 3083,
  2986. 3085, 3097, 3099, 3120, 3136, 3152, 3159, 3188, 3210, 3228, 3234, 3245, 3250, 3256, 3264, 3276,
  2987. 3281, 3296, 3349, 3363, 3378, 3392, 3395, 3420, 3440, 3461, 3488, 3529, 3531, 3584, 3588, 3591,
  2988. 3600, 3602, 3614, 3616, 3628, 3634, 3650, 3657, 3668, 3683, 3685, 3713, 3716, 3720, 3726, 3729,
  2989. 3736, 3753, 3778, 3802, 3805, 3819, 3841, 3845, 3851, 3856, 3880, 3922, 3938, 3970, 3993, 4032,
  2990. };
  2991. const int kmap_size = 4096;
  2992. const int nwant = grid_size == 256 ? 2 : 3;
  2993. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  2994. uint32_t * kgrid_q3xs;
  2995. int * kmap_q3xs;
  2996. uint16_t * kneighbors_q3xs;
  2997. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  2998. uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
  2999. for (int k = 0; k < grid_size; ++k) {
  3000. int8_t * pos = (int8_t *)(the_grid + k);
  3001. for (int i = 0; i < 4; ++i) {
  3002. int l = (kgrid[k] >> 3*i) & 0x7;
  3003. pos[i] = 2*l + 1;
  3004. }
  3005. }
  3006. kgrid_q3xs = the_grid;
  3007. iq3_data[gindex].grid = the_grid;
  3008. kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
  3009. iq3_data[gindex].map = kmap_q3xs;
  3010. for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
  3011. uint32_t aux32;
  3012. uint8_t * aux8 = (uint8_t *)&aux32;
  3013. for (int i = 0; i < grid_size; ++i) {
  3014. aux32 = kgrid_q3xs[i];
  3015. uint16_t index = 0;
  3016. for (int k=0; k<4; ++k) {
  3017. uint16_t q = (aux8[k] - 1)/2;
  3018. index |= (q << 3*k);
  3019. }
  3020. kmap_q3xs[index] = i;
  3021. }
  3022. int8_t pos[4];
  3023. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  3024. int num_neighbors = 0, num_not_in_map = 0;
  3025. for (int i = 0; i < kmap_size; ++i) {
  3026. if (kmap_q3xs[i] >= 0) continue;
  3027. ++num_not_in_map;
  3028. for (int k = 0; k < 4; ++k) {
  3029. int l = (i >> 3*k) & 0x7;
  3030. pos[k] = 2*l + 1;
  3031. }
  3032. for (int j = 0; j < grid_size; ++j) {
  3033. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  3034. int d2 = 0;
  3035. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  3036. dist2[2*j+0] = d2;
  3037. dist2[2*j+1] = j;
  3038. }
  3039. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  3040. int n = 0; int d2 = dist2[0];
  3041. int nhave = 1;
  3042. for (int j = 0; j < grid_size; ++j) {
  3043. if (dist2[2*j] > d2) {
  3044. if (nhave == nwant) break;
  3045. d2 = dist2[2*j];
  3046. ++nhave;
  3047. }
  3048. ++n;
  3049. }
  3050. num_neighbors += n;
  3051. }
  3052. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  3053. kneighbors_q3xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  3054. iq3_data[gindex].neighbours = kneighbors_q3xs;
  3055. int counter = 0;
  3056. for (int i = 0; i < kmap_size; ++i) {
  3057. if (kmap_q3xs[i] >= 0) continue;
  3058. for (int k = 0; k < 4; ++k) {
  3059. int l = (i >> 3*k) & 0x7;
  3060. pos[k] = 2*l + 1;
  3061. }
  3062. for (int j = 0; j < grid_size; ++j) {
  3063. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  3064. int d2 = 0;
  3065. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  3066. dist2[2*j+0] = d2;
  3067. dist2[2*j+1] = j;
  3068. }
  3069. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  3070. kmap_q3xs[i] = -(counter + 1);
  3071. int d2 = dist2[0];
  3072. uint16_t * start = &kneighbors_q3xs[counter++];
  3073. int n = 0, nhave = 1;
  3074. for (int j = 0; j < grid_size; ++j) {
  3075. if (dist2[2*j] > d2) {
  3076. if (nhave == nwant) break;
  3077. d2 = dist2[2*j];
  3078. ++nhave;
  3079. }
  3080. kneighbors_q3xs[counter++] = dist2[2*j+1];
  3081. ++n;
  3082. }
  3083. *start = n;
  3084. }
  3085. free(dist2);
  3086. }
  3087. void iq3xs_free_impl(int grid_size) {
  3088. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  3089. const int gindex = iq3_data_index(grid_size);
  3090. if (iq3_data[gindex].grid) {
  3091. free(iq3_data[gindex].grid); iq3_data[gindex].grid = NULL;
  3092. free(iq3_data[gindex].map); iq3_data[gindex].map = NULL;
  3093. free(iq3_data[gindex].neighbours); iq3_data[gindex].neighbours = NULL;
  3094. }
  3095. }
  3096. static int iq3_find_best_neighbour(const uint16_t * GGML_RESTRICT neighbours, const uint32_t * GGML_RESTRICT grid,
  3097. const float * GGML_RESTRICT xval, const float * GGML_RESTRICT weight, float scale, int8_t * GGML_RESTRICT L) {
  3098. int num_neighbors = neighbours[0];
  3099. GGML_ASSERT(num_neighbors > 0);
  3100. float best_d2 = FLT_MAX;
  3101. int grid_index = -1;
  3102. for (int j = 1; j <= num_neighbors; ++j) {
  3103. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  3104. float d2 = 0;
  3105. for (int i = 0; i < 4; ++i) {
  3106. float q = pg[i];
  3107. float diff = scale*q - xval[i];
  3108. d2 += weight[i]*diff*diff;
  3109. }
  3110. if (d2 < best_d2) {
  3111. best_d2 = d2; grid_index = neighbours[j];
  3112. }
  3113. }
  3114. GGML_ASSERT(grid_index >= 0);
  3115. const int8_t * pg = (const int8_t *)(grid + grid_index);
  3116. for (int i = 0; i < 4; ++i) L[i] = (pg[i] - 1)/2;
  3117. return grid_index;
  3118. }
  3119. static void quantize_row_iq3_xxs_impl(int grid_size, const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t n,
  3120. const float * GGML_RESTRICT quant_weights) {
  3121. const int gindex = iq3_data_index(grid_size);
  3122. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  3123. const int * kmap_q3xs = iq3_data[gindex].map;
  3124. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  3125. //GGML_ASSERT(quant_weights && "missing quantization weights");
  3126. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  3127. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  3128. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  3129. GGML_ASSERT(n%QK_K == 0);
  3130. const int kMaxQ = 8;
  3131. const int64_t nbl = n/QK_K;
  3132. ggml_fp16_t * dh;
  3133. uint8_t * qs;
  3134. int block_size;
  3135. if (grid_size == 256) {
  3136. block_iq3_xxs * y = vy;
  3137. dh = &y->d;
  3138. qs = y->qs;
  3139. block_size = sizeof(block_iq3_xxs);
  3140. } else {
  3141. block_iq3_s * y = vy;
  3142. dh = &y->d;
  3143. qs = y->qs;
  3144. block_size = sizeof(block_iq3_s);
  3145. }
  3146. int quant_size = block_size - sizeof(ggml_fp16_t);
  3147. float scales[QK_K/32];
  3148. float weight[32];
  3149. float xval[32];
  3150. int8_t L[32];
  3151. int8_t Laux[32];
  3152. float waux[32];
  3153. bool is_on_grid[8];
  3154. bool is_on_grid_aux[8];
  3155. uint8_t block_signs[8];
  3156. uint8_t q3[3*(QK_K/8)+QK_K/32];
  3157. uint32_t * scales_and_signs = (uint32_t *)(q3 + QK_K/4);
  3158. uint8_t * qh = q3 + 3*(QK_K/8);
  3159. for (int ibl = 0; ibl < nbl; ++ibl) {
  3160. dh[0] = GGML_FP32_TO_FP16(0.f);
  3161. memset(q3, 0, 3*QK_K/8+QK_K/32);
  3162. float max_scale = 0;
  3163. const float * xbl = x + QK_K*ibl;
  3164. float sumx2 = 0;
  3165. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  3166. float sigma2 = 2*sumx2/QK_K;
  3167. for (int ib = 0; ib < QK_K/32; ++ib) {
  3168. const float * xb = xbl + 32*ib;
  3169. if (quant_weights) {
  3170. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  3171. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  3172. } else {
  3173. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  3174. }
  3175. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  3176. for (int k = 0; k < 4; ++k) {
  3177. int nflip = 0;
  3178. uint8_t s = 0;
  3179. for (int i = 0; i < 8; ++i) {
  3180. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  3181. else {
  3182. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  3183. }
  3184. }
  3185. if (nflip%2) {
  3186. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  3187. for (int i = 1; i < 8; ++i) {
  3188. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  3189. if (ax < min) {
  3190. min = ax; imin = i;
  3191. }
  3192. }
  3193. xval[8*k+imin] = -xval[8*k+imin];
  3194. s ^= (1 << imin);
  3195. }
  3196. block_signs[k] = s & 127;
  3197. }
  3198. float max = xval[0];
  3199. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  3200. if (max < GROUP_MAX_EPS_IQ3_XXS) {
  3201. scales[ib] = 0;
  3202. memset(L, 0, 32);
  3203. continue;
  3204. }
  3205. float best = 0;
  3206. float scale = max/(2*kMaxQ-1);
  3207. for (int is = -15; is <= 15; ++is) {
  3208. float id = (2*kMaxQ-1+is*0.2f)/max;
  3209. float this_scale = 1/id;
  3210. for (int k = 0; k < 8; ++k) {
  3211. for (int i = 0; i < 4; ++i) {
  3212. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  3213. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  3214. }
  3215. uint16_t u = 0;
  3216. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  3217. int grid_index = kmap_q3xs[u];
  3218. is_on_grid_aux[k] = true;
  3219. if (grid_index < 0) {
  3220. is_on_grid_aux[k] = false;
  3221. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  3222. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  3223. }
  3224. }
  3225. float sumqx = 0, sumq2 = 0;
  3226. for (int i = 0; i < 32; ++i) {
  3227. float w = weight[i];
  3228. float q = 2*Laux[i] + 1;
  3229. sumqx += w*xval[i]*q;
  3230. sumq2 += w*q*q;
  3231. }
  3232. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  3233. scale = sumqx/sumq2; best = scale*sumqx;
  3234. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  3235. for (int k = 0; k < 8; ++k) is_on_grid[k] = is_on_grid_aux[k];
  3236. }
  3237. }
  3238. int n_not_ongrid = 0;
  3239. for (int k = 0; k < 8; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  3240. if (n_not_ongrid > 0 && scale > 0) {
  3241. float id = 1/scale;
  3242. for (int k = 0; k < 8; ++k) {
  3243. if (is_on_grid[k]) continue;
  3244. uint16_t u = 0;
  3245. for (int i = 0; i < 4; ++i) {
  3246. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  3247. l = MAX(0, MIN(kMaxQ-1, l));
  3248. u |= (l << 3*i);
  3249. }
  3250. int grid_index = kmap_q3xs[u];
  3251. if (grid_index < 0) {
  3252. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  3253. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  3254. }
  3255. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  3256. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  3257. }
  3258. float sumqx = 0, sumq2 = 0;
  3259. for (int i = 0; i < 32; ++i) {
  3260. float w = weight[i];
  3261. float q = 2*L[i] + 1;
  3262. sumqx += w*xval[i]*q;
  3263. sumq2 += w*q*q;
  3264. }
  3265. if (sumq2 > 0) scale = sumqx/sumq2;
  3266. }
  3267. if (scale < 0) {
  3268. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  3269. // and correspondingly flip quant signs.
  3270. scale = -scale;
  3271. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  3272. }
  3273. for (int k = 0; k < 8; ++k) {
  3274. uint16_t u = 0;
  3275. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  3276. int grid_index = kmap_q3xs[u];
  3277. if (grid_index < 0) {
  3278. printf("Oops: found point %u not on grid:", u);
  3279. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  3280. printf("\n");
  3281. GGML_ABORT("fatal error");
  3282. }
  3283. if (grid_size == 256) {
  3284. q3[8*ib+k] = grid_index;
  3285. } else {
  3286. q3[8*ib+k] = grid_index & 255;
  3287. qh[ib] |= ((grid_index >> 8) << k);
  3288. }
  3289. }
  3290. scales_and_signs[ib] = block_signs[0] | (block_signs[1] << 7) | (block_signs[2] << 14) | (block_signs[3] << 21);
  3291. GGML_ASSERT(scale >= 0);
  3292. scales[ib] = scale;
  3293. max_scale = MAX(max_scale, scale);
  3294. }
  3295. if (!max_scale) {
  3296. memset(qs, 0, quant_size);
  3297. dh += block_size/sizeof(ggml_fp16_t);
  3298. qs += block_size;
  3299. continue;
  3300. }
  3301. float d = max_scale/31;
  3302. dh[0] = GGML_FP32_TO_FP16(d * 1.0125f); // small improvement via this fudge factor
  3303. float id = 1/d;
  3304. for (int ib = 0; ib < QK_K/32; ++ib) {
  3305. int l = nearest_int(0.5f*(id*scales[ib]-1));
  3306. l = MAX(0, MIN(15, l));
  3307. scales_and_signs[ib] |= ((uint32_t)l << 28);
  3308. }
  3309. memcpy(qs, q3, quant_size);
  3310. dh += block_size/sizeof(ggml_fp16_t);
  3311. qs += block_size;
  3312. }
  3313. }
  3314. size_t quantize_iq3_xxs(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  3315. GGML_ASSERT(n_per_row%QK_K == 0);
  3316. int64_t nblock = n_per_row/QK_K;
  3317. char * qrow = (char *)dst;
  3318. for (int64_t row = 0; row < nrow; ++row) {
  3319. quantize_row_iq3_xxs_impl(256, src, qrow, n_per_row, quant_weights);
  3320. src += n_per_row;
  3321. qrow += nblock*sizeof(block_iq3_xxs);
  3322. }
  3323. return nrow * nblock * sizeof(block_iq3_xxs);
  3324. }
  3325. void quantize_row_iq3_xxs_ref(const float * GGML_RESTRICT x, block_iq3_xxs * GGML_RESTRICT y, int64_t k) {
  3326. assert(k % QK_K == 0);
  3327. quantize_row_iq3_xxs_impl(256, x, y, k, NULL);
  3328. }
  3329. static void quantize_row_iq3_s_impl(int block_size, const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int n,
  3330. const float * GGML_RESTRICT quant_weights,
  3331. float * scales,
  3332. float * weight,
  3333. float * xval,
  3334. int8_t * L,
  3335. int8_t * Laux,
  3336. float * waux,
  3337. bool * is_on_grid,
  3338. bool * is_on_grid_aux,
  3339. uint8_t * block_signs) {
  3340. const int gindex = iq3_data_index(512);
  3341. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  3342. const int * kmap_q3xs = iq3_data[gindex].map;
  3343. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  3344. //GGML_ASSERT(quant_weights && "missing quantization weights");
  3345. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  3346. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  3347. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  3348. GGML_ASSERT(n%QK_K == 0);
  3349. const int kMaxQ = 8;
  3350. const int64_t nbl = n/QK_K;
  3351. block_iq3_s * y = vy;
  3352. const int bs4 = block_size/4;
  3353. const int bs8 = block_size/8;
  3354. for (int ibl = 0; ibl < nbl; ++ibl) {
  3355. memset(&y[ibl], 0, sizeof(block_iq3_s));
  3356. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  3357. uint8_t * qs = y[ibl].qs;
  3358. uint8_t * qh = y[ibl].qh;
  3359. uint8_t * signs = y[ibl].signs;
  3360. float max_scale = 0;
  3361. const float * xbl = x + QK_K*ibl;
  3362. float sumx2 = 0;
  3363. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  3364. float sigma2 = 2*sumx2/QK_K;
  3365. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  3366. const float * xb = xbl + block_size*ib;
  3367. if (quant_weights) {
  3368. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  3369. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  3370. } else {
  3371. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  3372. }
  3373. for (int i = 0; i < block_size; ++i) waux[i] = sqrtf(weight[i]);
  3374. for (int k = 0; k < bs8; ++k) {
  3375. uint8_t s = 0;
  3376. for (int i = 0; i < 8; ++i) {
  3377. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  3378. else {
  3379. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  3380. }
  3381. }
  3382. block_signs[k] = s;
  3383. }
  3384. float max = xval[0];
  3385. for (int i = 1; i < block_size; ++i) max = MAX(max, xval[i]);
  3386. if (!max) {
  3387. scales[ib] = 0;
  3388. continue;
  3389. }
  3390. float best = 0;
  3391. float scale = max/(2*kMaxQ-1);
  3392. for (int k = 0; k < bs4; ++k) is_on_grid[k] = false;
  3393. for (int is = -9; is <= 9; ++is) {
  3394. float id = (2*kMaxQ-1+is*0.2f)/max;
  3395. float this_scale = 1/id;
  3396. for (int k = 0; k < bs4; ++k) {
  3397. for (int i = 0; i < 4; ++i) {
  3398. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  3399. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  3400. }
  3401. uint16_t u = 0;
  3402. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  3403. int grid_index = kmap_q3xs[u];
  3404. is_on_grid_aux[k] = true;
  3405. if (grid_index < 0) {
  3406. is_on_grid_aux[k] = false;
  3407. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  3408. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  3409. }
  3410. }
  3411. float sumqx = 0, sumq2 = 0;
  3412. for (int i = 0; i < block_size; ++i) {
  3413. float w = weight[i];
  3414. float q = 2*Laux[i] + 1;
  3415. sumqx += w*xval[i]*q;
  3416. sumq2 += w*q*q;
  3417. }
  3418. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  3419. scale = sumqx/sumq2; best = scale*sumqx;
  3420. for (int i = 0; i < block_size; ++i) L[i] = Laux[i];
  3421. for (int k = 0; k < bs4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  3422. }
  3423. }
  3424. int n_not_ongrid = 0;
  3425. for (int k = 0; k < bs4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  3426. if (n_not_ongrid > 0 && scale > 0) {
  3427. float id = 1/scale;
  3428. for (int k = 0; k < bs4; ++k) {
  3429. //if (is_on_grid[k]) continue;
  3430. uint16_t u = 0;
  3431. for (int i = 0; i < 4; ++i) {
  3432. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  3433. l = MAX(0, MIN(kMaxQ-1, l));
  3434. u |= (l << 3*i);
  3435. }
  3436. int grid_index = kmap_q3xs[u];
  3437. if (grid_index < 0) {
  3438. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  3439. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  3440. }
  3441. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  3442. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  3443. }
  3444. float sumqx = 0, sumq2 = 0;
  3445. for (int i = 0; i < block_size; ++i) {
  3446. float w = weight[i];
  3447. float q = 2*L[i] + 1;
  3448. sumqx += w*xval[i]*q;
  3449. sumq2 += w*q*q;
  3450. }
  3451. if (sumq2 > 0) scale = sumqx/sumq2;
  3452. }
  3453. if (scale < 0) {
  3454. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  3455. // and correspondingly flip quant signs.
  3456. scale = -scale;
  3457. for (int k = 0; k < bs8; ++k) block_signs[k] = ~block_signs[k];
  3458. }
  3459. for (int k = 0; k < bs4; ++k) {
  3460. uint16_t u = 0;
  3461. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  3462. int grid_index = kmap_q3xs[u];
  3463. if (grid_index < 0) {
  3464. printf("Oops: found point %u not on grid:", u);
  3465. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  3466. printf("\n");
  3467. GGML_ABORT("fatal error");
  3468. }
  3469. qs[k] = grid_index & 255;
  3470. qh[(ib*bs4+k)/8] |= ((grid_index >> 8) << ((ib*bs4+k)%8));
  3471. }
  3472. qs += bs4;
  3473. for (int k = 0; k < bs8; ++k) signs[k] = block_signs[k];
  3474. signs += bs8;
  3475. GGML_ASSERT(scale >= 0);
  3476. scales[ib] = scale;
  3477. max_scale = MAX(max_scale, scale);
  3478. }
  3479. if (!max_scale) {
  3480. continue;
  3481. }
  3482. float d = max_scale/31;
  3483. y[ibl].d = GGML_FP32_TO_FP16(d * 1.033f);
  3484. float id = 1/d;
  3485. for (int ib = 0; ib < QK_K/block_size; ib += 2) {
  3486. int l1 = nearest_int(0.5f*(id*scales[ib+0]-1));
  3487. l1 = MAX(0, MIN(15, l1));
  3488. int l2 = nearest_int(0.5f*(id*scales[ib+1]-1));
  3489. l2 = MAX(0, MIN(15, l2));
  3490. y[ibl].scales[ib/2] = l1 | (l2 << 4);
  3491. }
  3492. }
  3493. }
  3494. #define IQ3S_BLOCK_SIZE 32
  3495. size_t quantize_iq3_s(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  3496. GGML_ASSERT(n_per_row%QK_K == 0);
  3497. int64_t nblock = n_per_row/QK_K;
  3498. float scales[QK_K/IQ3S_BLOCK_SIZE];
  3499. float weight[IQ3S_BLOCK_SIZE];
  3500. float xval[IQ3S_BLOCK_SIZE];
  3501. int8_t L[IQ3S_BLOCK_SIZE];
  3502. int8_t Laux[IQ3S_BLOCK_SIZE];
  3503. float waux[IQ3S_BLOCK_SIZE];
  3504. bool is_on_grid[IQ3S_BLOCK_SIZE/4];
  3505. bool is_on_grid_aux[IQ3S_BLOCK_SIZE/4];
  3506. uint8_t block_signs[IQ3S_BLOCK_SIZE/8];
  3507. char * qrow = (char *)dst;
  3508. for (int64_t row = 0; row < nrow; ++row) {
  3509. quantize_row_iq3_s_impl(IQ3S_BLOCK_SIZE, src, qrow, n_per_row, quant_weights,
  3510. scales, weight, xval, L, Laux, waux, is_on_grid, is_on_grid_aux, block_signs);
  3511. src += n_per_row;
  3512. qrow += nblock*sizeof(block_iq3_s);
  3513. }
  3514. return nrow * nblock * sizeof(block_iq3_s);
  3515. }
  3516. void quantize_row_iq3_s_ref(const float * GGML_RESTRICT x, block_iq3_s * GGML_RESTRICT y, int64_t k) {
  3517. assert(k % QK_K == 0);
  3518. quantize_iq3_s(x, y, 1, k, NULL);
  3519. }
  3520. // =================================== 1.5 bpw ===================================================
  3521. static int iq1_find_best_neighbour(const uint16_t * GGML_RESTRICT neighbours, const uint64_t * GGML_RESTRICT grid,
  3522. const float * GGML_RESTRICT xval, const float * GGML_RESTRICT weight, float * scale, int8_t * GGML_RESTRICT L, int ngrid) {
  3523. int num_neighbors = neighbours[0];
  3524. GGML_ASSERT(num_neighbors > 0);
  3525. float best_score = -FLT_MAX;
  3526. int grid_index = -1;
  3527. for (int j = 1; j <= num_neighbors; ++j) {
  3528. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  3529. float sumqx = 0, sumq2 = 0;
  3530. for (int i = 0; i < 8; ++i) {
  3531. float q = (pg[i] - 3)/2;
  3532. float w = weight[i];
  3533. sumqx += w*q*xval[i];
  3534. sumq2 += w*q*q;
  3535. }
  3536. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  3537. *scale = sumqx/sumq2; best_score = *scale * sumqx;
  3538. grid_index = neighbours[j];
  3539. }
  3540. }
  3541. if (grid_index < 0) {
  3542. for (int i = 0; i < ngrid; ++i) {
  3543. const int8_t * grid_i = (const int8_t *)(grid + i);
  3544. float sumqx = 0, sumq2 = 0;
  3545. for (int j = 0; j < 8; ++j) {
  3546. float w = weight[j];
  3547. float q = (grid_i[j] - 3)/2;
  3548. sumqx += w*q*xval[j];
  3549. sumq2 += w*q*q;
  3550. }
  3551. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  3552. *scale = sumqx/sumq2; best_score = *scale*sumqx;
  3553. grid_index = i;
  3554. }
  3555. }
  3556. }
  3557. if (grid_index < 0) {
  3558. printf("Oops, did not find grid point\n");
  3559. printf("Have %d neighbours\n", num_neighbors);
  3560. for (int j = 1; j <= num_neighbors; ++j) {
  3561. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  3562. float sumqx = 0, sumq2 = 0;
  3563. for (int i = 0; i < 8; ++i) {
  3564. float q = (pg[i] - 3)/2;
  3565. float w = weight[i];
  3566. sumqx += w*q*xval[i];
  3567. sumq2 += w*q*q;
  3568. }
  3569. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  3570. }
  3571. }
  3572. GGML_ASSERT(grid_index >= 0);
  3573. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  3574. *scale *= 1.05f; // This is a fudge factor. Don't ask me why it improves the result.
  3575. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  3576. const int8_t * pg = (const int8_t *)(grid + grid_index);
  3577. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  3578. return grid_index;
  3579. }
  3580. static int iq1_find_best_neighbour2(const uint16_t * GGML_RESTRICT neighbours, const uint64_t * GGML_RESTRICT grid,
  3581. const float * GGML_RESTRICT xval, const float * GGML_RESTRICT weight, float scale, const float * GGML_RESTRICT xg, int8_t * GGML_RESTRICT L, int ngrid) {
  3582. int num_neighbors = neighbours[0];
  3583. GGML_ASSERT(num_neighbors > 0);
  3584. float best_score = FLT_MAX;
  3585. int grid_index = -1;
  3586. for (int j = 1; j <= num_neighbors; ++j) {
  3587. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  3588. float d2 = 0;
  3589. for (int i = 0; i < 8; ++i) {
  3590. float q = xg[(pg[i] - 1)/2];
  3591. float w = weight[i];
  3592. float diff = scale*q - xval[i];
  3593. d2 += w*diff*diff;
  3594. }
  3595. if (d2 < best_score) {
  3596. best_score = d2;
  3597. grid_index = neighbours[j];
  3598. }
  3599. }
  3600. if (grid_index < 0) {
  3601. for (int i = 0; i < ngrid; ++i) {
  3602. const int8_t * grid_i = (const int8_t *)(grid + i);
  3603. float d2 = 0;
  3604. for (int j = 0; j < 8; ++j) {
  3605. float w = weight[j];
  3606. float q = xg[(grid_i[j] - 1)/2];
  3607. float diff = scale*q - xval[i];
  3608. d2 += w*diff*diff;
  3609. }
  3610. if (d2 < best_score) {
  3611. best_score = d2;
  3612. grid_index = i;
  3613. }
  3614. }
  3615. }
  3616. if (grid_index < 0) {
  3617. printf("Oops, did not find grid point\n");
  3618. printf("Have %d neighbours\n", num_neighbors);
  3619. for (int j = 1; j <= num_neighbors; ++j) {
  3620. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  3621. float sumqx = 0, sumq2 = 0;
  3622. for (int i = 0; i < 8; ++i) {
  3623. float q = xg[(pg[i] - 1)/2];
  3624. float w = weight[i];
  3625. sumqx += w*q*xval[i];
  3626. sumq2 += w*q*q;
  3627. }
  3628. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  3629. }
  3630. }
  3631. GGML_ASSERT(grid_index >= 0);
  3632. const int8_t * pg = (const int8_t *)(grid + grid_index);
  3633. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  3634. return grid_index;
  3635. }
  3636. static int iq1_sort_helper(const void * left, const void * right) {
  3637. const float * l = left;
  3638. const float * r = right;
  3639. return *l < *r ? -1 : *l > *r ? 1 : 0;
  3640. }
  3641. #define IQ1S_BLOCK_SIZE 32
  3642. #define IQ1M_BLOCK_SIZE 16
  3643. static void quantize_row_iq1_s_impl(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t n, const float * GGML_RESTRICT quant_weights,
  3644. float * scales,
  3645. float * weight,
  3646. float * sumx,
  3647. float * sumw,
  3648. float * pairs,
  3649. int8_t * L,
  3650. uint16_t * index,
  3651. int8_t * shifts) {
  3652. const int gindex = iq2_data_index(GGML_TYPE_IQ1_S);
  3653. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  3654. const int * kmap_q2xs = iq2_data[gindex].map;
  3655. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  3656. GGML_ASSERT(quant_weights && "missing quantization weights");
  3657. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  3658. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  3659. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  3660. GGML_ASSERT(n%QK_K == 0);
  3661. block_iq1_s * y = vy;
  3662. const int64_t nbl = n/QK_K;
  3663. const int block_size = IQ1S_BLOCK_SIZE;
  3664. const float x_p[3] = {-1 + IQ1S_DELTA, IQ1S_DELTA, 1 + IQ1S_DELTA};
  3665. const float x_m[3] = {-1 - IQ1S_DELTA, -IQ1S_DELTA, 1 - IQ1S_DELTA};
  3666. int * idx = (int *)(pairs + 1);
  3667. for (int ibl = 0; ibl < nbl; ++ibl) {
  3668. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  3669. memset(y[ibl].qs, 0, QK_K/8);
  3670. memset(y[ibl].qh, 0, QK_K/16);
  3671. float max_scale = 0;
  3672. const float * xbl = x + QK_K*ibl;
  3673. float sumx2 = 0;
  3674. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  3675. float sigma2 = 2*sumx2/QK_K;
  3676. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  3677. const float * xb = xbl + block_size*ib;
  3678. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  3679. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  3680. float max = fabsf(xb[0]);
  3681. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  3682. if (max < GROUP_MAX_EPS_IQ1_S) {
  3683. scales[ib] = 0;
  3684. memset(L, 1, block_size);
  3685. continue;
  3686. }
  3687. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  3688. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  3689. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  3690. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  3691. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  3692. // for each possible and score for each split.
  3693. for (int j = 0; j < block_size; ++j) {
  3694. pairs[2*j] = xb[j];
  3695. idx[2*j] = j;
  3696. }
  3697. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  3698. {
  3699. sumx[0] = sumw[0] = 0;
  3700. for (int j = 0; j < block_size; ++j) {
  3701. int i = idx[2*j];
  3702. sumx[j+1] = sumx[j] + weight[i]*xb[i];
  3703. sumw[j+1] = sumw[j] + weight[i];
  3704. }
  3705. }
  3706. float best_score = -FLT_MIN, scale = max;
  3707. int besti1 = -1, besti2 = -1, best_shift = 0;
  3708. for (int i1 = 0; i1 <= block_size; ++i1) {
  3709. for (int i2 = i1; i2 <= block_size; ++i2) {
  3710. float sumqx = (sumx[i1] - sumx[0])*x_p[0] + (sumx[i2] - sumx[i1])*x_p[1] + (sumx[block_size] - sumx[i2])*x_p[2];
  3711. float sumq2 = (sumw[i1] - sumw[0])*x_p[0]*x_p[0] + (sumw[i2] - sumw[i1])*x_p[1]*x_p[1] + (sumw[block_size] - sumw[i2])*x_p[2]*x_p[2];
  3712. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  3713. scale = sumqx/sumq2; best_score = scale*sumqx;
  3714. besti1 = i1; besti2 = i2; best_shift = 1;
  3715. }
  3716. sumqx = (sumx[i1] - sumx[0])*x_m[0] + (sumx[i2] - sumx[i1])*x_m[1] + (sumx[block_size] - sumx[i2])*x_m[2];
  3717. sumq2 = (sumw[i1] - sumw[0])*x_m[0]*x_m[0] + (sumw[i2] - sumw[i1])*x_m[1]*x_m[1] + (sumw[block_size] - sumw[i2])*x_m[2]*x_m[2];
  3718. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  3719. scale = sumqx/sumq2; best_score = scale*sumqx;
  3720. besti1 = i1; besti2 = i2; best_shift = -1;
  3721. }
  3722. }
  3723. }
  3724. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_shift != 0);
  3725. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  3726. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  3727. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  3728. if (scale < 0) {
  3729. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  3730. scale = -scale; best_shift = -best_shift;
  3731. }
  3732. bool all_on_grid = true;
  3733. const float * xx = best_shift == 1 ? x_p : x_m;
  3734. for (int k = 0; k < block_size/8; ++k) {
  3735. uint16_t u = 0;
  3736. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  3737. int grid_index = kmap_q2xs[u];
  3738. if (grid_index < 0) {
  3739. all_on_grid = false;
  3740. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  3741. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  3742. GGML_ASSERT(grid_index >= 0);
  3743. }
  3744. index[k] = grid_index;
  3745. }
  3746. if (!all_on_grid) {
  3747. float sumqx = 0, sumq2 = 0;
  3748. for (int k = 0; k < block_size/8; ++k) {
  3749. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  3750. for (int j = 0; j < 8; ++j) {
  3751. float w = weight[8*k + j];
  3752. float q = xx[(pg[j] - 1)/2];
  3753. sumqx += w*q*xb[8*k+j];
  3754. sumq2 += w*q*q;
  3755. }
  3756. }
  3757. if (sumqx > 0 && sumq2 > 0) scale = sumqx/sumq2;
  3758. }
  3759. uint16_t h = 0;
  3760. for (int k = 0; k < block_size/8; ++k) {
  3761. y[ibl].qs[(block_size/8)*ib + k] = index[k] & 255;
  3762. h |= (index[k] >> 8) << 3*k;
  3763. }
  3764. y[ibl].qh[ib] = h;
  3765. GGML_ASSERT(scale >= 0);
  3766. scales[ib] = scale;
  3767. shifts[ib] = best_shift;
  3768. max_scale = MAX(max_scale, scale);
  3769. }
  3770. if (!max_scale) {
  3771. continue;
  3772. }
  3773. float d = max_scale/15;
  3774. y[ibl].d = GGML_FP32_TO_FP16(d*1.125f); // 1.125f is another fudge factor. Don't ask me why it is needed.
  3775. float id = 1/d;
  3776. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  3777. int l = nearest_int(0.5f*(id*scales[ib]-1));
  3778. l = MAX(0, MIN(7, l));
  3779. if (shifts[ib] == -1) l |= 8;
  3780. y[ibl].qh[ib] |= (l << 12);
  3781. }
  3782. }
  3783. }
  3784. size_t quantize_iq1_s(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  3785. GGML_ASSERT(n_per_row%QK_K == 0);
  3786. float scales[QK_K/IQ1S_BLOCK_SIZE];
  3787. float weight[IQ1S_BLOCK_SIZE];
  3788. int8_t L[IQ1S_BLOCK_SIZE];
  3789. float sumx[IQ1S_BLOCK_SIZE+1];
  3790. float sumw[IQ1S_BLOCK_SIZE+1];
  3791. float pairs[2*IQ1S_BLOCK_SIZE];
  3792. uint16_t index[IQ1S_BLOCK_SIZE/8];
  3793. int8_t shifts[QK_K/IQ1S_BLOCK_SIZE];
  3794. int64_t nblock = n_per_row/QK_K;
  3795. char * qrow = (char *)dst;
  3796. for (int64_t row = 0; row < nrow; ++row) {
  3797. quantize_row_iq1_s_impl(src, qrow, n_per_row, quant_weights, scales, weight, sumx, sumw, pairs, L, index, shifts);
  3798. src += n_per_row;
  3799. qrow += nblock*sizeof(block_iq1_s);
  3800. }
  3801. return nrow * nblock * sizeof(block_iq1_s);
  3802. }
  3803. static void quantize_row_iq1_m_impl(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t n, const float * GGML_RESTRICT quant_weights,
  3804. float * scales,
  3805. float * weight,
  3806. float * pairs,
  3807. int8_t * L,
  3808. uint16_t * index,
  3809. int8_t * shifts) {
  3810. const int gindex = iq2_data_index(GGML_TYPE_IQ1_M);
  3811. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  3812. const int * kmap_q2xs = iq2_data[gindex].map;
  3813. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  3814. //GGML_ASSERT(quant_weights && "missing quantization weights");
  3815. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  3816. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  3817. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  3818. GGML_ASSERT(n%QK_K == 0);
  3819. block_iq1_m * y = vy;
  3820. const int64_t nbl = n/QK_K;
  3821. const int block_size = IQ1M_BLOCK_SIZE;
  3822. const float x_p[3] = {-1 + IQ1M_DELTA, IQ1M_DELTA, 1 + IQ1M_DELTA};
  3823. const float x_m[3] = {-1 - IQ1M_DELTA, -IQ1M_DELTA, 1 - IQ1M_DELTA};
  3824. const uint8_t masks[4] = {0x00, 0x80, 0x08, 0x88};
  3825. int * idx = (int *)(pairs + 1);
  3826. float sumqx[4], sumq2[4];
  3827. iq1m_scale_t s;
  3828. const float * xx;
  3829. for (int ibl = 0; ibl < nbl; ++ibl) {
  3830. memset(y[ibl].qs, 0, QK_K/8);
  3831. memset(y[ibl].qh, 0, QK_K/16);
  3832. memset(y[ibl].scales, 0, QK_K/32);
  3833. float max_scale = 0;
  3834. const float * xbl = x + QK_K*ibl;
  3835. float sumx2 = 0;
  3836. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  3837. float sigma2 = 2*sumx2/QK_K;
  3838. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  3839. const float * xb = xbl + block_size*ib;
  3840. if (quant_weights) {
  3841. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  3842. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  3843. } else {
  3844. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  3845. }
  3846. float max = fabsf(xb[0]);
  3847. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  3848. if (max < GROUP_MAX_EPS_IQ1_M) {
  3849. scales[ib] = 0;
  3850. memset(L, 1, block_size);
  3851. continue;
  3852. }
  3853. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  3854. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  3855. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  3856. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  3857. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  3858. // for each possible and score for each split.
  3859. for (int j = 0; j < block_size; ++j) {
  3860. pairs[2*j] = xb[j];
  3861. idx[2*j] = j;
  3862. }
  3863. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  3864. float best_score = -FLT_MIN, scale = max;
  3865. int besti1 = -1, besti2 = -1, best_k = -1;
  3866. // 0: +, +
  3867. // 1: +, -
  3868. // 2: -, +
  3869. // 3: -, -
  3870. for (int i1 = 0; i1 <= block_size; ++i1) {
  3871. for (int i2 = i1; i2 <= block_size; ++i2) {
  3872. memset(sumqx, 0, 4*sizeof(float));
  3873. memset(sumq2, 0, 4*sizeof(float));
  3874. for (int j = 0; j < i1; ++j) {
  3875. int i = idx[2*j];
  3876. if (i < block_size/2) {
  3877. sumqx[0] += weight[i]*x_p[0]*xb[i];
  3878. sumqx[1] += weight[i]*x_p[0]*xb[i];
  3879. sumqx[2] += weight[i]*x_m[0]*xb[i];
  3880. sumqx[3] += weight[i]*x_m[0]*xb[i];
  3881. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  3882. sumq2[1] += weight[i]*x_p[0]*x_p[0];
  3883. sumq2[2] += weight[i]*x_m[0]*x_m[0];
  3884. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  3885. } else {
  3886. sumqx[0] += weight[i]*x_p[0]*xb[i];
  3887. sumqx[2] += weight[i]*x_p[0]*xb[i];
  3888. sumqx[1] += weight[i]*x_m[0]*xb[i];
  3889. sumqx[3] += weight[i]*x_m[0]*xb[i];
  3890. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  3891. sumq2[2] += weight[i]*x_p[0]*x_p[0];
  3892. sumq2[1] += weight[i]*x_m[0]*x_m[0];
  3893. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  3894. }
  3895. }
  3896. for (int j = i1; j < i2; ++j) {
  3897. int i = idx[2*j];
  3898. if (i < block_size/2) {
  3899. sumqx[0] += weight[i]*x_p[1]*xb[i];
  3900. sumqx[1] += weight[i]*x_p[1]*xb[i];
  3901. sumqx[2] += weight[i]*x_m[1]*xb[i];
  3902. sumqx[3] += weight[i]*x_m[1]*xb[i];
  3903. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  3904. sumq2[1] += weight[i]*x_p[1]*x_p[1];
  3905. sumq2[2] += weight[i]*x_m[1]*x_m[1];
  3906. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  3907. } else {
  3908. sumqx[0] += weight[i]*x_p[1]*xb[i];
  3909. sumqx[2] += weight[i]*x_p[1]*xb[i];
  3910. sumqx[1] += weight[i]*x_m[1]*xb[i];
  3911. sumqx[3] += weight[i]*x_m[1]*xb[i];
  3912. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  3913. sumq2[2] += weight[i]*x_p[1]*x_p[1];
  3914. sumq2[1] += weight[i]*x_m[1]*x_m[1];
  3915. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  3916. }
  3917. }
  3918. for (int j = i2; j < block_size; ++j) {
  3919. int i = idx[2*j];
  3920. if (i < block_size/2) {
  3921. sumqx[0] += weight[i]*x_p[2]*xb[i];
  3922. sumqx[1] += weight[i]*x_p[2]*xb[i];
  3923. sumqx[2] += weight[i]*x_m[2]*xb[i];
  3924. sumqx[3] += weight[i]*x_m[2]*xb[i];
  3925. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  3926. sumq2[1] += weight[i]*x_p[2]*x_p[2];
  3927. sumq2[2] += weight[i]*x_m[2]*x_m[2];
  3928. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  3929. } else {
  3930. sumqx[0] += weight[i]*x_p[2]*xb[i];
  3931. sumqx[2] += weight[i]*x_p[2]*xb[i];
  3932. sumqx[1] += weight[i]*x_m[2]*xb[i];
  3933. sumqx[3] += weight[i]*x_m[2]*xb[i];
  3934. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  3935. sumq2[2] += weight[i]*x_p[2]*x_p[2];
  3936. sumq2[1] += weight[i]*x_m[2]*x_m[2];
  3937. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  3938. }
  3939. }
  3940. for (int k = 0; k < 4; ++k) {
  3941. if (sumq2[k] > 0 && sumqx[k]*sumqx[k] > best_score*sumq2[k]) {
  3942. scale = sumqx[k]/sumq2[k]; best_score = scale*sumqx[k];
  3943. besti1 = i1; besti2 = i2; best_k = k;
  3944. }
  3945. }
  3946. }
  3947. }
  3948. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_k >= 0);
  3949. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  3950. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  3951. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  3952. if (scale < 0) {
  3953. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  3954. scale = -scale;
  3955. best_k = best_k == 0 ? 3 : best_k == 1 ? 2 : best_k == 2 ? 1 : 0;
  3956. }
  3957. bool all_on_grid = true;
  3958. for (int k = 0; k < block_size/8; ++k) {
  3959. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  3960. else xx = best_k%2 == 0 ? x_p : x_m;
  3961. uint16_t u = 0;
  3962. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  3963. int grid_index = kmap_q2xs[u];
  3964. if (grid_index < 0) {
  3965. all_on_grid = false;
  3966. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  3967. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  3968. GGML_ASSERT(grid_index >= 0);
  3969. }
  3970. index[k] = grid_index;
  3971. }
  3972. if (!all_on_grid) {
  3973. float sumqx_f = 0, sumq2_f = 0;
  3974. for (int k = 0; k < block_size/8; ++k) {
  3975. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  3976. else xx = best_k%2 == 0 ? x_p : x_m;
  3977. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  3978. for (int j = 0; j < 8; ++j) {
  3979. float w = weight[8*k + j];
  3980. float q = xx[(pg[j] - 1)/2];
  3981. sumqx_f += w*q*xb[8*k+j];
  3982. sumq2_f += w*q*q;
  3983. }
  3984. }
  3985. if (sumqx_f > 0 && sumq2_f > 0) scale = sumqx_f/sumq2_f;
  3986. }
  3987. y[ibl].qs[2*ib + 0] = index[0] & 255;
  3988. y[ibl].qs[2*ib + 1] = index[1] & 255;
  3989. y[ibl].qh[ib] = (index[0] >> 8) | ((index[1] >> 8) << 4);
  3990. GGML_ASSERT(scale >= 0);
  3991. scales[ib] = scale;
  3992. shifts[ib] = best_k;
  3993. max_scale = MAX(max_scale, scale);
  3994. }
  3995. if (!max_scale) {
  3996. continue;
  3997. }
  3998. uint16_t * sc = (uint16_t *)y[ibl].scales;
  3999. float d = max_scale/15;
  4000. float id = 1/d;
  4001. float sumqx_f = 0, sumq2_f = 0;
  4002. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  4003. int l = nearest_int(0.5f*(id*scales[ib+0]-1));
  4004. l = MAX(0, MIN(7, l));
  4005. sc[ib/4] |= (l << 3*(ib%4));
  4006. y[ibl].qh[ib] |= masks[shifts[ib]];
  4007. const float * xb = xbl + block_size*ib;
  4008. if (quant_weights) {
  4009. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  4010. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  4011. } else {
  4012. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  4013. }
  4014. for (int k = 0; k < block_size/8; ++k) {
  4015. if (k == 0) xx = shifts[ib] < 2 ? x_p : x_m;
  4016. else xx = shifts[ib]%2 == 0 ? x_p : x_m;
  4017. const int8_t * pg = (const int8_t *)(kgrid_q2xs + y[ibl].qs[2*ib+k] + ((y[ibl].qh[ib] << (8 - 4*k)) & 0x700));
  4018. for (int j = 0; j < 8; ++j) {
  4019. float w = weight[8*k + j];
  4020. float q = xx[(pg[j] - 1)/2]*(2*l+1);
  4021. sumqx_f += w*q*xb[8*k+j];
  4022. sumq2_f += w*q*q;
  4023. }
  4024. }
  4025. }
  4026. if (sumq2_f > 0) d = sumqx_f/sumq2_f;
  4027. s.f16 = GGML_FP32_TO_FP16(d*1.1125f); // 1.1125f is another fudge factor. Don't ask me why it is needed.
  4028. sc[0] |= ((s.u16 & 0x000f) << 12);
  4029. sc[1] |= ((s.u16 & 0x00f0) << 8);
  4030. sc[2] |= ((s.u16 & 0x0f00) << 4);
  4031. sc[3] |= ((s.u16 & 0xf000) << 0);
  4032. }
  4033. }
  4034. size_t quantize_iq1_m(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  4035. GGML_ASSERT(n_per_row%QK_K == 0);
  4036. float scales[QK_K/IQ1M_BLOCK_SIZE];
  4037. float weight[IQ1M_BLOCK_SIZE];
  4038. int8_t L[IQ1M_BLOCK_SIZE];
  4039. float pairs[2*IQ1M_BLOCK_SIZE];
  4040. uint16_t index[IQ1M_BLOCK_SIZE/8];
  4041. int8_t shifts[QK_K/IQ1M_BLOCK_SIZE];
  4042. int64_t nblock = n_per_row/QK_K;
  4043. char * qrow = (char *)dst;
  4044. for (int64_t row = 0; row < nrow; ++row) {
  4045. quantize_row_iq1_m_impl(src, qrow, n_per_row, quant_weights, scales, weight, pairs, L, index, shifts);
  4046. src += n_per_row;
  4047. qrow += nblock*sizeof(block_iq1_m);
  4048. }
  4049. return nrow * nblock * sizeof(block_iq1_m);
  4050. }
  4051. // ============================ 4-bit non-linear quants
  4052. static inline int best_index_int8(int n, const int8_t * val, float x) {
  4053. if (x <= val[0]) return 0;
  4054. if (x >= val[n-1]) return n-1;
  4055. int ml = 0, mu = n-1;
  4056. while (mu-ml > 1) {
  4057. int mav = (ml+mu)/2;
  4058. if (x < val[mav]) mu = mav; else ml = mav;
  4059. }
  4060. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  4061. }
  4062. static void quantize_row_iq4_nl_impl(const int super_block_size, const int block_size, const float * GGML_RESTRICT x,
  4063. ggml_fp16_t * dh, uint8_t * q4, uint16_t * scales_h, uint8_t * scales_l,
  4064. float * scales, float * weight, uint8_t * L,
  4065. const int8_t * values,
  4066. const float * quant_weights,
  4067. const int ntry) {
  4068. float sigma2 = 0;
  4069. for (int j = 0; j < super_block_size; ++j) sigma2 += x[j]*x[j];
  4070. sigma2 *= 2.f/super_block_size;
  4071. memset(q4, 0, super_block_size/2);
  4072. dh[0] = GGML_FP32_TO_FP16(0.f);
  4073. float max_scale = 0, amax_scale = 0;
  4074. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  4075. const float * xb = x + ib*block_size;
  4076. uint8_t * Lb = L + ib*block_size;
  4077. if (quant_weights) {
  4078. const float * qw = quant_weights + ib*block_size;
  4079. for (int j = 0; j < block_size; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  4080. } else {
  4081. for (int j = 0; j < block_size; ++j) weight[j] = xb[j]*xb[j];
  4082. }
  4083. float amax = 0, max = 0;
  4084. for (int j = 0; j < block_size; ++j) {
  4085. float ax = fabsf(xb[j]);
  4086. if (ax > amax) {
  4087. amax = ax; max = xb[j];
  4088. }
  4089. }
  4090. if (amax < GROUP_MAX_EPS) {
  4091. scales[ib] = 0;
  4092. continue;
  4093. }
  4094. float d = ntry > 0 ? -max/values[0] : max/values[0];
  4095. float id = 1/d;
  4096. float sumqx = 0, sumq2 = 0;
  4097. for (int j = 0; j < block_size; ++j) {
  4098. float al = id*xb[j];
  4099. int l = best_index_int8(16, values, al);
  4100. Lb[j] = l;
  4101. float q = values[l];
  4102. float w = weight[j];
  4103. sumqx += w*q*xb[j];
  4104. sumq2 += w*q*q;
  4105. }
  4106. d = sumqx/sumq2;
  4107. float best = d*sumqx;
  4108. for (int itry = -ntry; itry <= ntry; ++itry) {
  4109. id = (itry + values[0])/max;
  4110. sumqx = sumq2 = 0;
  4111. for (int j = 0; j < block_size; ++j) {
  4112. float al = id*xb[j];
  4113. int l = best_index_int8(16, values, al);
  4114. float q = values[l];
  4115. float w = weight[j];
  4116. sumqx += w*q*xb[j];
  4117. sumq2 += w*q*q;
  4118. }
  4119. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  4120. d = sumqx/sumq2; best = d * sumqx;
  4121. }
  4122. }
  4123. scales[ib] = d;
  4124. float abs_d = fabsf(d);
  4125. if (abs_d > amax_scale) {
  4126. amax_scale = abs_d; max_scale = d;
  4127. }
  4128. }
  4129. if (super_block_size/block_size > 1) {
  4130. int nb = super_block_size/block_size;
  4131. memset(scales_h, 0, ((nb+7)/8)*sizeof(uint16_t));
  4132. float d = -max_scale/32;
  4133. dh[0] = GGML_FP32_TO_FP16(d);
  4134. float id = d ? 1/d : 0.f;
  4135. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  4136. int l = nearest_int(id*scales[ib]);
  4137. l = MAX(-32, MIN(31, l));
  4138. float dl = d * l;
  4139. float idl = dl ? 1/dl : 0.f;
  4140. uint8_t * Lb = L + ib*block_size;
  4141. const float * xb = x + ib*block_size;
  4142. for (int j = 0; j < block_size; ++j) {
  4143. Lb[j] = best_index_int8(16, values, idl*xb[j]);
  4144. }
  4145. l += 32;
  4146. uint8_t l_l = l & 0xf;
  4147. uint8_t l_h = l >> 4;
  4148. if (ib%2 == 0) scales_l[ib/2] = l_l;
  4149. else scales_l[ib/2] |= (l_l << 4);
  4150. scales_h[ib/8] |= (l_h << 2*(ib%8));
  4151. }
  4152. } else {
  4153. dh[0] = GGML_FP32_TO_FP16(scales[0]);
  4154. if (ntry > 0) {
  4155. float id = scales[0] ? 1/scales[0] : 0;
  4156. for (int j = 0; j < super_block_size; ++j) {
  4157. L[j] = best_index_int8(16, values, id*x[j]);
  4158. }
  4159. }
  4160. }
  4161. for (int i = 0; i < super_block_size/32; ++i) {
  4162. for (int j = 0; j < 16; ++j) {
  4163. q4[16*i + j] = L[32*i + j] | (L[32*i + 16 + j] << 4);
  4164. }
  4165. }
  4166. }
  4167. size_t quantize_iq4_nl(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  4168. GGML_ASSERT(n_per_row%QK4_NL == 0);
  4169. int64_t nblock = n_per_row/QK4_NL;
  4170. char * qrow = (char *)dst;
  4171. uint8_t L[QK4_NL];
  4172. float weight[QK4_NL];
  4173. uint16_t unused_h;
  4174. uint8_t * unused_l = NULL;
  4175. float scale;
  4176. for (int64_t row = 0; row < nrow; ++row) {
  4177. block_iq4_nl * iq4 = (block_iq4_nl *)qrow;
  4178. for (int ibl = 0; ibl < nblock; ++ibl) {
  4179. const float * qw = quant_weights ? quant_weights + QK4_NL*ibl : NULL;
  4180. quantize_row_iq4_nl_impl(QK4_NL, 32, src + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  4181. &scale, weight, L, kvalues_iq4nl, qw, 7);
  4182. }
  4183. src += n_per_row;
  4184. qrow += nblock*sizeof(block_iq4_nl);
  4185. }
  4186. return nrow * nblock * sizeof(block_iq4_nl);
  4187. }
  4188. //void quantize_row_iq4_nl_ref(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t k) {
  4189. void quantize_row_iq4_nl_ref(const float * GGML_RESTRICT x, block_iq4_nl * GGML_RESTRICT y, int64_t k) {
  4190. GGML_ASSERT(k%QK4_NL == 0);
  4191. int64_t nblock = k/QK4_NL;
  4192. uint8_t L[QK4_NL];
  4193. float weight[QK4_NL];
  4194. uint16_t unused_h;
  4195. uint8_t * unused_l = NULL;
  4196. float scale;
  4197. block_iq4_nl * iq4 = y;
  4198. for (int ibl = 0; ibl < nblock; ++ibl) {
  4199. quantize_row_iq4_nl_impl(QK4_NL, 32, x + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  4200. &scale, weight, L, kvalues_iq4nl, NULL, -1);
  4201. }
  4202. }
  4203. size_t quantize_iq4_xs(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  4204. GGML_ASSERT(n_per_row%QK_K == 0);
  4205. int64_t nblock = n_per_row/QK_K;
  4206. char * qrow = (char *)dst;
  4207. uint8_t L[QK_K];
  4208. float weight[32];
  4209. float scales[QK_K/32];
  4210. for (int64_t row = 0; row < nrow; ++row) {
  4211. block_iq4_xs * iq4 = (block_iq4_xs *)qrow;
  4212. for (int ibl = 0; ibl < nblock; ++ibl) {
  4213. const float * qw = quant_weights ? quant_weights + QK_K*ibl : NULL;
  4214. quantize_row_iq4_nl_impl(QK_K, 32, src + QK_K*ibl, &iq4[ibl].d, iq4[ibl].qs, &iq4[ibl].scales_h, iq4[ibl].scales_l,
  4215. scales, weight, L, kvalues_iq4nl, qw, 7);
  4216. }
  4217. src += n_per_row;
  4218. qrow += nblock*sizeof(block_iq4_xs);
  4219. }
  4220. return nrow * nblock * sizeof(block_iq4_xs);
  4221. }
  4222. void quantize_row_iq4_xs_ref(const float * GGML_RESTRICT x, block_iq4_xs * GGML_RESTRICT y, int64_t k) {
  4223. assert(k % QK_K == 0);
  4224. quantize_iq4_xs(x, y, 1, k, NULL);
  4225. }
  4226. // =============================== 2.5625 bpw
  4227. static void quantize_row_iq2_s_impl(const float * GGML_RESTRICT x, void * GGML_RESTRICT vy, int64_t n, const float * GGML_RESTRICT quant_weights) {
  4228. const int gindex = iq2_data_index(GGML_TYPE_IQ2_S);
  4229. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  4230. const int * kmap_q2xs = iq2_data[gindex].map;
  4231. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  4232. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  4233. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  4234. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  4235. GGML_ASSERT(n%QK_K == 0);
  4236. const int kMaxQ = 3;
  4237. const int64_t nbl = n/QK_K;
  4238. block_iq2_s * y = vy;
  4239. float scales[QK_K/16];
  4240. float weight[16];
  4241. float xval[16];
  4242. int8_t L[16];
  4243. int8_t Laux[16];
  4244. float waux[16];
  4245. bool is_on_grid[2];
  4246. bool is_on_grid_aux[2];
  4247. uint8_t block_signs[2];
  4248. for (int ibl = 0; ibl < nbl; ++ibl) {
  4249. memset(&y[ibl], 0, sizeof(block_iq2_s));
  4250. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  4251. float max_scale = 0;
  4252. const float * xbl = x + QK_K*ibl;
  4253. float sumx2 = 0;
  4254. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  4255. float sigma2 = 2*sumx2/QK_K;
  4256. for (int ib = 0; ib < QK_K/16; ++ib) {
  4257. const float * xb = xbl + 16*ib;
  4258. if (quant_weights) {
  4259. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  4260. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  4261. } else {
  4262. for (int i = 0; i < 16; ++i) weight[i] = 0.25f*sigma2 + xb[i]*xb[i];
  4263. }
  4264. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  4265. for (int k = 0; k < 2; ++k) {
  4266. uint8_t s = 0;
  4267. for (int i = 0; i < 8; ++i) {
  4268. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  4269. else {
  4270. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  4271. }
  4272. }
  4273. block_signs[k] = s;
  4274. }
  4275. float max = xval[0];
  4276. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  4277. if (max < GROUP_MAX_EPS_IQ2_S) {
  4278. scales[ib] = 0;
  4279. continue;
  4280. }
  4281. float best = 0;
  4282. float scale = max/(2*kMaxQ-1);
  4283. is_on_grid[0] = is_on_grid[1] = true;
  4284. for (int is = -9; is <= 9; ++is) {
  4285. float id = (2*kMaxQ-1+is*0.1f)/max;
  4286. float this_scale = 1/id;
  4287. for (int k = 0; k < 2; ++k) {
  4288. for (int i = 0; i < 8; ++i) {
  4289. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  4290. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  4291. }
  4292. uint16_t u = 0;
  4293. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  4294. int grid_index = kmap_q2xs[u];
  4295. is_on_grid_aux[k] = true;
  4296. if (grid_index < 0) {
  4297. is_on_grid_aux[k] = false;
  4298. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  4299. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  4300. }
  4301. }
  4302. float sumqx = 0, sumq2 = 0;
  4303. for (int i = 0; i < 16; ++i) {
  4304. float w = weight[i];
  4305. float q = 2*Laux[i] + 1;
  4306. sumqx += w*xval[i]*q;
  4307. sumq2 += w*q*q;
  4308. }
  4309. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  4310. scale = sumqx/sumq2; best = scale*sumqx;
  4311. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  4312. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  4313. }
  4314. }
  4315. int n_not_ongrid = 0;
  4316. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  4317. if (n_not_ongrid > 0 && scale > 0) {
  4318. float id = 1/scale;
  4319. for (int k = 0; k < 2; ++k) {
  4320. if (is_on_grid[k]) continue;
  4321. uint16_t u = 0;
  4322. for (int i = 0; i < 8; ++i) {
  4323. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  4324. l = MAX(0, MIN(kMaxQ-1, l));
  4325. u |= (l << 2*i);
  4326. L[8*k + i] = l;
  4327. }
  4328. int grid_index = kmap_q2xs[u];
  4329. if (grid_index < 0) {
  4330. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  4331. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  4332. }
  4333. }
  4334. float sumqx = 0, sumq2 = 0;
  4335. for (int i = 0; i < 16; ++i) {
  4336. float w = weight[i];
  4337. float q = 2*L[i] + 1;
  4338. sumqx += w*xval[i]*q;
  4339. sumq2 += w*q*q;
  4340. }
  4341. if (sumq2 > 0) scale = sumqx/sumq2;
  4342. }
  4343. if (scale < 0) {
  4344. scale = -scale;
  4345. for (int k = 0; k < 2; ++k) block_signs[k] = ~block_signs[k];
  4346. }
  4347. for (int k = 0; k < 2; ++k) {
  4348. uint16_t u = 0;
  4349. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  4350. int grid_index = kmap_q2xs[u];
  4351. if (grid_index < 0) {
  4352. printf("Oops: found point %u not on grid:", u);
  4353. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  4354. printf("\n");
  4355. GGML_ABORT("fatal error");
  4356. }
  4357. const int i8 = 2*ib + k;
  4358. y[ibl].qs[i8] = grid_index & 255;
  4359. y[ibl].qh[i8/4] |= ((grid_index >> 8) << 2*(i8%4));
  4360. y[ibl].qs[QK_K/8 + i8] = block_signs[k];
  4361. }
  4362. GGML_ASSERT(scale >= 0);
  4363. scales[ib] = scale;
  4364. max_scale = MAX(max_scale, scale);
  4365. }
  4366. if (!max_scale) {
  4367. continue;
  4368. }
  4369. float d = max_scale/31;
  4370. y[ibl].d = GGML_FP32_TO_FP16(d * 0.9875f);
  4371. float id = 1/d;
  4372. for (int ib = 0; ib < QK_K/16; ++ib) {
  4373. int l = nearest_int(0.5f*(id*scales[ib]-1));
  4374. l = MAX(0, MIN(15, l));
  4375. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  4376. else y[ibl].scales[ib/2] |= (l << 4);
  4377. }
  4378. }
  4379. }
  4380. size_t quantize_iq2_s(const float * GGML_RESTRICT src, void * GGML_RESTRICT dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  4381. GGML_ASSERT(n_per_row%QK_K == 0);
  4382. int64_t nblock = n_per_row/QK_K;
  4383. char * qrow = (char *)dst;
  4384. for (int64_t row = 0; row < nrow; ++row) {
  4385. quantize_row_iq2_s_impl(src, qrow, n_per_row, quant_weights);
  4386. src += n_per_row;
  4387. qrow += nblock*sizeof(block_iq2_s);
  4388. }
  4389. return nrow * nblock * sizeof(block_iq2_s);
  4390. }
  4391. void quantize_row_iq2_s_ref(const float * GGML_RESTRICT x, block_iq2_s * GGML_RESTRICT y, int64_t k) {
  4392. assert(k % QK_K == 0);
  4393. quantize_iq2_s(x, y, 1, k, NULL);
  4394. }
  4395. // =============================== data validation
  4396. static bool validate_float(float f, size_t i) {
  4397. if (isinf(f)) {
  4398. fprintf(stderr, "ggml_validate_row_data: found inf value at block %zu\n", i);
  4399. return false;
  4400. }
  4401. if (isnan(f)) {
  4402. fprintf(stderr, "ggml_validate_row_data: found nan value at block %zu\n", i);
  4403. return false;
  4404. }
  4405. return true;
  4406. }
  4407. static bool isinf_fp16(ggml_fp16_t f) {
  4408. return (f & 0x7c00) == 0x7c00 && (f & 0x03ff) == 0;
  4409. }
  4410. static bool isnan_fp16(ggml_fp16_t f) {
  4411. return (f & 0x7c00) == 0x7c00 && (f & 0x03ff) != 0;
  4412. }
  4413. static bool validate_fp16(ggml_fp16_t f, size_t i) {
  4414. if (isinf_fp16(f)) {
  4415. fprintf(stderr, "ggml_validate_row_data: found inf value at block %zu\n", i);
  4416. return false;
  4417. }
  4418. if (isnan_fp16(f)) {
  4419. fprintf(stderr, "ggml_validate_row_data: found nan value at block %zu\n", i);
  4420. return false;
  4421. }
  4422. return true;
  4423. }
  4424. #define VALIDATE_ROW_DATA_D_F16_IMPL(type, data, nb) \
  4425. const type * q = (const type *) (data); \
  4426. for (size_t i = 0; i < (nb); ++i) { \
  4427. if (!validate_fp16(q[i].d, i)) { \
  4428. return false; \
  4429. } \
  4430. }
  4431. #define VALIDATE_ROW_DATA_DM_F16_IMPL(type, data, nb, d, m) \
  4432. const type * q = (const type *) (data); \
  4433. for (size_t i = 0; i < (nb); ++i) { \
  4434. if (!validate_fp16(q[i].d, i) || !validate_fp16(q[i].m, i)) { \
  4435. return false; \
  4436. } \
  4437. }
  4438. #define VALIDATE_ROW_DATA_DVEC_F16_IMPL(type, data, nb, nr) \
  4439. const type * q = (const type *) (data); \
  4440. for (size_t i = 0; i < (nb); ++i) { \
  4441. for (size_t j = 0; j < (nr); ++j) { \
  4442. if (!validate_fp16(q[i].d[j], i)) { \
  4443. return false; \
  4444. } \
  4445. } \
  4446. }
  4447. bool ggml_validate_row_data(enum ggml_type type, const void * data, size_t nbytes) {
  4448. if (type < 0 || type >= GGML_TYPE_COUNT) {
  4449. fprintf(stderr, "%s: invalid type %d\n", __func__, type);
  4450. return false;
  4451. }
  4452. if (nbytes % ggml_type_size(type) != 0) {
  4453. fprintf(stderr, "%s: invalid size %zu for type %s (type size = %zu)\n", __func__, nbytes, ggml_type_name(type), ggml_type_size(type));
  4454. return false;
  4455. }
  4456. const size_t nb = nbytes/ggml_type_size(type);
  4457. switch (type) {
  4458. case GGML_TYPE_BF16:
  4459. {
  4460. int nans = 0;
  4461. int infs = 0;
  4462. const unsigned short * f = (const unsigned short *) data;
  4463. for (size_t i = 0; i < nb; ++i) {
  4464. nans += (f[i] & 0x7fff) > 0x7f80;
  4465. infs += (f[i] & 0x7fff) == 0x7f80;
  4466. }
  4467. if (nans) {
  4468. fprintf(stderr, "%s: found %d NaNs in row of %zu BF16 values\n", __func__, nans, nb);
  4469. return false;
  4470. }
  4471. if (infs) {
  4472. fprintf(stderr, "%s: found %d infinities in row of %zu BF16 values\n", __func__, infs, nb);
  4473. return false;
  4474. }
  4475. } break;
  4476. case GGML_TYPE_F16:
  4477. {
  4478. const ggml_fp16_t * f = (const ggml_fp16_t *) data;
  4479. size_t i = 0;
  4480. #if defined(__AVX2__)
  4481. for (; i + 15 < nb; i += 16) {
  4482. __m256i v = _mm256_loadu_si256((const __m256i *)(f + i));
  4483. __m256i vexp = _mm256_and_si256(v, _mm256_set1_epi16(0x7c00));
  4484. __m256i cmp = _mm256_cmpeq_epi16(vexp, _mm256_set1_epi16(0x7c00));
  4485. int mask = _mm256_movemask_epi8(cmp);
  4486. if (mask) {
  4487. for (size_t j = 0; j < 16; ++j) {
  4488. if (!validate_fp16(f[i + j], i + j)) {
  4489. return false;
  4490. }
  4491. }
  4492. GGML_UNREACHABLE();
  4493. }
  4494. }
  4495. #elif defined(__ARM_NEON)
  4496. for (; i + 7 < nb; i += 8) {
  4497. uint16x8_t v = vld1q_u16(f + i);
  4498. uint16x8_t vexp = vandq_u16(v, vdupq_n_u16(0x7c00));
  4499. uint16x8_t cmp = vceqq_u16(vexp, vdupq_n_u16(0x7c00));
  4500. uint64_t mask = vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(cmp, 4)), 0);
  4501. if (mask) {
  4502. for (size_t j = 0; j < 8; ++j) {
  4503. if (!validate_fp16(f[i + j], i + j)) {
  4504. return false;
  4505. }
  4506. }
  4507. GGML_UNREACHABLE();
  4508. }
  4509. }
  4510. #endif
  4511. for (; i < nb; ++i) {
  4512. if (!validate_fp16(f[i], i)) {
  4513. return false;
  4514. }
  4515. }
  4516. } break;
  4517. case GGML_TYPE_F32:
  4518. {
  4519. const float * f = (const float *) data;
  4520. size_t i = 0;
  4521. #if defined(__AVX2__)
  4522. for (; i + 7 < nb; i += 8) {
  4523. __m256i v = _mm256_loadu_si256((const __m256i *)(f + i));
  4524. __m256i vexp = _mm256_and_si256(v, _mm256_set1_epi32(0x7f800000));
  4525. __m256i cmp = _mm256_cmpeq_epi32(vexp, _mm256_set1_epi32(0x7f800000));
  4526. int mask = _mm256_movemask_epi8(cmp);
  4527. if (mask) {
  4528. for (size_t j = 0; j < 8; ++j) {
  4529. if (!validate_float(f[i + j], i + j)) {
  4530. return false;
  4531. }
  4532. }
  4533. GGML_UNREACHABLE();
  4534. }
  4535. }
  4536. #elif defined(__ARM_NEON)
  4537. for (; i + 3 < nb; i += 4) {
  4538. uint32x4_t v = vld1q_u32((const uint32_t *)f + i);
  4539. uint32x4_t vexp = vandq_u32(v, vdupq_n_u32(0x7f800000));
  4540. uint32x4_t cmp = vceqq_u32(vexp, vdupq_n_u32(0x7f800000));
  4541. uint64_t mask = vget_lane_u64(vreinterpret_u64_u16(vshrn_n_u32(cmp, 8)), 0);
  4542. if (mask) {
  4543. for (size_t j = 0; j < 4; ++j) {
  4544. if (!validate_float(f[i + j], i + j)) {
  4545. return false;
  4546. }
  4547. }
  4548. GGML_UNREACHABLE();
  4549. }
  4550. }
  4551. #endif
  4552. for (; i < nb; ++i) {
  4553. if (!validate_float(f[i], i)) {
  4554. return false;
  4555. }
  4556. }
  4557. } break;
  4558. case GGML_TYPE_F64:
  4559. {
  4560. const double * f = (const double *) data;
  4561. for (size_t i = 0; i < nb; ++i) {
  4562. if (!validate_float(f[i], i)) {
  4563. return false;
  4564. }
  4565. }
  4566. } break;
  4567. case GGML_TYPE_Q4_0:
  4568. {
  4569. VALIDATE_ROW_DATA_D_F16_IMPL(block_q4_0, data, nb);
  4570. } break;
  4571. case GGML_TYPE_Q4_1:
  4572. {
  4573. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q4_1, data, nb, d, m);
  4574. } break;
  4575. case GGML_TYPE_Q5_0:
  4576. {
  4577. VALIDATE_ROW_DATA_D_F16_IMPL(block_q5_0, data, nb);
  4578. } break;
  4579. case GGML_TYPE_Q5_1:
  4580. {
  4581. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q5_1, data, nb, d, m);
  4582. } break;
  4583. case GGML_TYPE_Q8_0:
  4584. {
  4585. VALIDATE_ROW_DATA_D_F16_IMPL(block_q8_0, data, nb);
  4586. } break;
  4587. case GGML_TYPE_Q2_K:
  4588. {
  4589. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q2_K, data, nb, d, dmin);
  4590. } break;
  4591. case GGML_TYPE_Q3_K:
  4592. {
  4593. VALIDATE_ROW_DATA_D_F16_IMPL(block_q3_K, data, nb);
  4594. } break;
  4595. case GGML_TYPE_Q4_K:
  4596. {
  4597. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q4_K, data, nb, d, dmin);
  4598. } break;
  4599. case GGML_TYPE_Q5_K:
  4600. {
  4601. VALIDATE_ROW_DATA_DM_F16_IMPL(block_q5_K, data, nb, d, dmin);
  4602. } break;
  4603. case GGML_TYPE_Q6_K:
  4604. {
  4605. VALIDATE_ROW_DATA_D_F16_IMPL(block_q6_K, data, nb);
  4606. } break;
  4607. case GGML_TYPE_Q8_K:
  4608. {
  4609. const block_q8_K * q = (const block_q8_K *) data;
  4610. for (size_t i = 0; i < nb; ++i) {
  4611. if (!validate_float(q[i].d, i)) {
  4612. return false;
  4613. }
  4614. }
  4615. } break;
  4616. case GGML_TYPE_TQ1_0:
  4617. {
  4618. VALIDATE_ROW_DATA_D_F16_IMPL(block_tq1_0, data, nb);
  4619. } break;
  4620. case GGML_TYPE_TQ2_0:
  4621. {
  4622. VALIDATE_ROW_DATA_D_F16_IMPL(block_tq2_0, data, nb);
  4623. } break;
  4624. case GGML_TYPE_IQ1_S:
  4625. {
  4626. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq1_s, data, nb);
  4627. } break;
  4628. case GGML_TYPE_IQ1_M:
  4629. {
  4630. const block_iq1_m * q = (const block_iq1_m *) data;
  4631. for (size_t i = 0; i < nb; ++i) {
  4632. iq1m_scale_t scale;
  4633. const uint16_t * sc = (const uint16_t *)q[i].scales;
  4634. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  4635. if (!validate_fp16(scale.f16, i)) {
  4636. return false;
  4637. }
  4638. }
  4639. } break;
  4640. case GGML_TYPE_IQ2_XXS:
  4641. {
  4642. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_xxs, data, nb);
  4643. } break;
  4644. case GGML_TYPE_IQ2_XS:
  4645. {
  4646. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_xs, data, nb);
  4647. } break;
  4648. case GGML_TYPE_IQ2_S:
  4649. {
  4650. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq2_s, data, nb);
  4651. } break;
  4652. case GGML_TYPE_IQ3_XXS:
  4653. {
  4654. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq3_xxs, data, nb);
  4655. } break;
  4656. case GGML_TYPE_IQ3_S:
  4657. {
  4658. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq3_s, data, nb);
  4659. } break;
  4660. case GGML_TYPE_IQ4_XS:
  4661. {
  4662. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq4_xs, data, nb);
  4663. } break;
  4664. case GGML_TYPE_IQ4_NL:
  4665. {
  4666. VALIDATE_ROW_DATA_D_F16_IMPL(block_iq4_nl, data, nb);
  4667. } break;
  4668. case GGML_TYPE_I8:
  4669. case GGML_TYPE_I16:
  4670. case GGML_TYPE_I32:
  4671. case GGML_TYPE_I64:
  4672. // nothing to validate
  4673. break;
  4674. default:
  4675. {
  4676. fprintf(stderr, "%s: invalid type %d\n", __func__, type);
  4677. return false;
  4678. }
  4679. }
  4680. return true;
  4681. }