CompressedGemma commited on
Commit
3ba66cb
Β·
verified Β·
1 Parent(s): 94869ce

I prefer Q2 honestly.. but leaving IQ1 staging in for posterity

Browse files
Files changed (2) hide show
  1. hexstate_quantize.c +0 -891
  2. hexstate_requantize.py +14 -220
hexstate_quantize.c CHANGED
@@ -5239,853 +5239,6 @@ write_fail:
5239
  * ═══════════════════════════════════════════════════════════════════════════ */
5240
 
5241
  /* Initialize HExState subsystems (must be called once before quantization) */
5242
-
5243
- /* =====================================================================
5244
- * IQ1_S HPC QUANTIZATION (1-bit, imatrix-weighted)
5245
- *
5246
- * Same HPC front-end as the Q2_K path (hexstate_init with triality/s6,
5247
- * kurtosis-driven outlier clamp, OpenMP, imatrix importance, MSE error),
5248
- * with IQ1_S geometry: 256-element superblocks of 8x32 sub-blocks, each
5249
- * sub-block split by exhaustive 2-boundary 3-level search (the SIEVE
5250
- * sequential selector adapted to 1-bit), packed ggml-compatibly via the
5251
- * 1-bit grid (kgrid_1bit_2048 + kmap + neighbours, built once at init).
5252
- * Output layout matches ggml block_iq1_s: d(fp16) + qs[32] + qh[8].
5253
- * ===================================================================== */
5254
-
5255
- #define HEX_IQ1S_BLOCK_SIZE 32
5256
- #define HEX_NGRID_IQ1S 2048
5257
- #define HEX_KMAP_IQ1S_SIZE 43692
5258
- #define HEX_GROUP_MAX_EPS_IQ1_S 1e-12f
5259
- #define HEX_IQ1S_DELTA 0.125f
5260
-
5261
- static const uint16_t hex_kgrid_1bit_2048[2048] = {
5262
- 0, 2, 5, 8, 10, 17, 21, 32, 34, 40, 42, 69, 81, 84, 86, 101,
5263
- 128, 130, 136, 138, 149, 160, 162, 168, 170, 260, 261, 273, 276, 278, 281, 282,
5264
- 293, 321, 326, 329, 338, 341, 346, 353, 356, 358, 360, 389, 401, 404, 406, 421,
5265
- 512, 514, 520, 522, 533, 544, 546, 552, 554, 581, 593, 601, 612, 617, 640, 642,
5266
- 648, 650, 657, 661, 665, 672, 674, 680, 682, 1041, 1044, 1046, 1061, 1089, 1097, 1109,
5267
- 1114, 1124, 1125, 1169, 1177, 1189, 1281, 1284, 1285, 1286, 1301, 1304, 1306, 1321, 1344, 1349,
5268
- 1354, 1360, 1361, 1364, 1365, 1366, 1369, 1376, 1378, 1381, 1384, 1386, 1409, 1425, 1429, 1432,
5269
- 1434, 1441, 1444, 1445, 1446, 1449, 1556, 1561, 1601, 1604, 1616, 1618, 1621, 1624, 1632, 1633,
5270
- 1638, 1641, 1669, 1681, 1684, 1689, 2048, 2050, 2056, 2058, 2069, 2080, 2082, 2088, 2090, 2117,
5271
- 2129, 2134, 2149, 2176, 2178, 2184, 2186, 2197, 2208, 2210, 2216, 2218, 2309, 2321, 2324, 2329,
5272
- 2340, 2341, 2369, 2384, 2385, 2389, 2401, 2404, 2409, 2449, 2452, 2454, 2457, 2469, 2560, 2562,
5273
- 2568, 2570, 2581, 2592, 2594, 2600, 2602, 2629, 2641, 2649, 2657, 2661, 2688, 2690, 2693, 2696,
5274
- 2698, 2709, 2720, 2722, 2728, 2730, 4112, 4113, 4116, 4121, 4132, 4133, 4161, 4164, 4176, 4181,
5275
- 4184, 4193, 4196, 4197, 4201, 4241, 4244, 4246, 4257, 4261, 4353, 4356, 4358, 4361, 4368, 4370,
5276
- 4373, 4376, 4385, 4388, 4393, 4421, 4426, 4432, 4433, 4434, 4436, 4437, 4438, 4441, 4448, 4453,
5277
- 4484, 4498, 4501, 4513, 4516, 4625, 4628, 4630, 4645, 4672, 4678, 4681, 4690, 4693, 4696, 4698,
5278
- 4708, 4710, 4741, 4753, 4756, 4758, 4773, 5121, 5126, 5129, 5140, 5141, 5144, 5145, 5153, 5158,
5279
- 5185, 5189, 5190, 5192, 5194, 5201, 5204, 5205, 5206, 5209, 5218, 5221, 5224, 5252, 5257, 5264,
5280
- 5268, 5269, 5272, 5273, 5274, 5281, 5284, 5285, 5289, 5378, 5381, 5386, 5393, 5396, 5397, 5398,
5281
- 5401, 5408, 5410, 5413, 5416, 5418, 5441, 5444, 5445, 5446, 5457, 5458, 5460, 5461, 5462, 5465,
5282
- 5466, 5473, 5476, 5477, 5478, 5481, 5504, 5506, 5508, 5509, 5512, 5514, 5520, 5521, 5524, 5525,
5283
- 5526, 5529, 5530, 5536, 5538, 5541, 5633, 5636, 5637, 5638, 5653, 5654, 5656, 5658, 5665, 5670,
5284
- 5696, 5698, 5700, 5701, 5704, 5706, 5713, 5717, 5718, 5720, 5721, 5729, 5732, 5733, 5736, 5737,
5285
- 5738, 5766, 5770, 5778, 5781, 5796, 5801, 6161, 6166, 6181, 6209, 6212, 6214, 6217, 6224, 6229,
5286
- 6232, 6234, 6240, 6241, 6244, 6246, 6249, 6277, 6289, 6292, 6309, 6416, 6418, 6421, 6426, 6433,
5287
- 6437, 6466, 6468, 6469, 6472, 6481, 6484, 6485, 6486, 6489, 6490, 6496, 6501, 6506, 6537, 6545,
5288
- 6546, 6549, 6552, 6561, 6566, 6569, 6665, 6678, 6692, 6694, 6724, 6726, 6729, 6736, 6738, 6741,
5289
- 6744, 6753, 6758, 6761, 6789, 6801, 6806, 6810, 8192, 8194, 8200, 8202, 8213, 8224, 8226, 8229,
5290
- 8232, 8234, 8261, 8273, 8281, 8289, 8293, 8320, 8322, 8328, 8330, 8341, 8352, 8354, 8357, 8360,
5291
- 8362, 8453, 8465, 8468, 8473, 8485, 8514, 8516, 8521, 8533, 8536, 8538, 8545, 8548, 8549, 8550,
5292
- 8581, 8592, 8598, 8601, 8613, 8705, 8712, 8714, 8721, 8725, 8736, 8738, 8744, 8746, 8773, 8785,
5293
- 8790, 8793, 8805, 8833, 8840, 8842, 8849, 8853, 8864, 8866, 8872, 8874, 9221, 9236, 9238, 9241,
5294
- 9253, 9284, 9285, 9286, 9289, 9298, 9301, 9304, 9306, 9318, 9349, 9361, 9364, 9369, 9377, 9381,
5295
- 9481, 9493, 9505, 9513, 9536, 9541, 9544, 9553, 9556, 9557, 9561, 9570, 9573, 9576, 9609, 9616,
5296
- 9620, 9621, 9624, 9626, 9633, 9636, 9638, 9641, 9733, 9744, 9746, 9753, 9765, 9793, 9801, 9813,
5297
- 9824, 9825, 9833, 9860, 9862, 9872, 9882, 10240, 10242, 10248, 10250, 10261, 10272, 10274, 10280, 10282,
5298
- 10309, 10321, 10324, 10341, 10368, 10370, 10376, 10378, 10400, 10402, 10408, 10410, 10505, 10513, 10516, 10521,
5299
- 10533, 10566, 10569, 10578, 10581, 10593, 10596, 10598, 10601, 10629, 10640, 10646, 10649, 10660, 10661, 10752,
5300
- 10754, 10760, 10762, 10784, 10786, 10792, 10794, 10821, 10833, 10838, 10841, 10853, 10880, 10882, 10888, 10890,
5301
- 10901, 10912, 10914, 10920, 10922, 16389, 16401, 16406, 16421, 16457, 16466, 16469, 16472, 16474, 16481, 16484,
5302
- 16486, 16532, 16537, 16545, 16550, 16640, 16641, 16644, 16646, 16649, 16658, 16661, 16662, 16664, 16666, 16673,
5303
- 16678, 16681, 16709, 16712, 16714, 16721, 16724, 16725, 16726, 16729, 16730, 16741, 16744, 16746, 16769, 16772,
5304
- 16774, 16784, 16786, 16789, 16800, 16801, 16802, 16901, 16913, 16916, 16918, 16933, 16961, 16978, 16981, 16986,
5305
- 16996, 17001, 17033, 17044, 17061, 17409, 17429, 17433, 17449, 17477, 17480, 17482, 17489, 17492, 17493, 17494,
5306
- 17505, 17506, 17509, 17512, 17514, 17537, 17542, 17545, 17552, 17554, 17557, 17568, 17569, 17577, 17665, 17666,
5307
- 17669, 17674, 17681, 17684, 17685, 17686, 17689, 17696, 17701, 17706, 17729, 17732, 17733, 17734, 17737, 17744,
5308
- 17745, 17748, 17749, 17750, 17752, 17753, 17761, 17764, 17765, 17766, 17769, 17794, 17796, 17797, 17800, 17809,
5309
- 17812, 17813, 17814, 17817, 17818, 17829, 17832, 17834, 17921, 17925, 17929, 17940, 17941, 17944, 17946, 17953,
5310
- 17956, 17961, 17984, 17986, 17989, 17992, 18000, 18001, 18002, 18005, 18006, 18009, 18018, 18021, 18024, 18049,
5311
- 18053, 18058, 18068, 18069, 18081, 18084, 18086, 18437, 18449, 18453, 18458, 18469, 18498, 18505, 18512, 18517,
5312
- 18520, 18529, 18532, 18534, 18537, 18565, 18577, 18580, 18582, 18585, 18597, 18689, 18693, 18694, 18698, 18704,
5313
- 18708, 18709, 18712, 18721, 18724, 18726, 18752, 18757, 18762, 18769, 18770, 18772, 18773, 18774, 18777, 18784,
5314
- 18786, 18789, 18790, 18794, 18822, 18825, 18834, 18837, 18838, 18840, 18849, 18852, 18854, 18857, 18966, 19012,
5315
- 19014, 19017, 19029, 19032, 19034, 19044, 19049, 19092, 19109, 20481, 20484, 20485, 20486, 20489, 20498, 20501,
5316
- 20506, 20513, 20516, 20521, 20544, 20549, 20552, 20561, 20564, 20565, 20566, 20569, 20581, 20584, 20614, 20617,
5317
- 20629, 20632, 20640, 20641, 20646, 20649, 20741, 20744, 20745, 20746, 20753, 20756, 20757, 20758, 20760, 20761,
5318
- 20768, 20773, 20774, 20776, 20778, 20801, 20804, 20805, 20806, 20809, 20816, 20817, 20818, 20820, 20821, 20822,
5319
- 20824, 20825, 20826, 20833, 20836, 20837, 20838, 20841, 20866, 20869, 20881, 20884, 20885, 20886, 20889, 20896,
5320
- 20901, 20906, 20993, 20998, 21010, 21013, 21018, 21025, 21028, 21058, 21061, 21066, 21073, 21076, 21077, 21078,
5321
- 21081, 21090, 21093, 21125, 21136, 21138, 21141, 21145, 21146, 21156, 21508, 21509, 21521, 21524, 21525, 21526,
5322
- 21528, 21529, 21537, 21541, 21544, 21546, 21569, 21572, 21573, 21574, 21577, 21578, 21584, 21585, 21588, 21589,
5323
- 21590, 21592, 21593, 21594, 21601, 21602, 21604, 21605, 21606, 21609, 21632, 21640, 21642, 21649, 21652, 21653,
5324
- 21654, 21657, 21665, 21668, 21669, 21674, 21761, 21762, 21764, 21765, 21766, 21769, 21776, 21777, 21778, 21780,
5325
- 21781, 21782, 21785, 21786, 21793, 21796, 21797, 21798, 21801, 21824, 21825, 21826, 21828, 21829, 21830, 21832,
5326
- 21833, 21840, 21841, 21842, 21844, 21845, 21846, 21848, 21849, 21850, 21856, 21857, 21860, 21861, 21862, 21864,
5327
- 21865, 21866, 21889, 21892, 21893, 21897, 21898, 21904, 21905, 21908, 21909, 21910, 21912, 21913, 21921, 21924,
5328
- 21925, 21926, 21929, 22016, 22017, 22018, 22020, 22022, 22024, 22025, 22033, 22036, 22037, 22040, 22041, 22048,
5329
- 22049, 22050, 22052, 22053, 22054, 22056, 22057, 22081, 22085, 22086, 22088, 22089, 22090, 22096, 22097, 22098,
5330
- 22100, 22101, 22102, 22104, 22105, 22106, 22113, 22116, 22117, 22121, 22146, 22149, 22150, 22152, 22153, 22154,
5331
- 22161, 22165, 22170, 22178, 22181, 22182, 22184, 22185, 22532, 22533, 22534, 22537, 22544, 22549, 22552, 22561,
5332
- 22570, 22597, 22600, 22602, 22609, 22612, 22613, 22614, 22616, 22617, 22624, 22626, 22628, 22629, 22658, 22665,
5333
- 22672, 22674, 22677, 22680, 22689, 22697, 22785, 22786, 22789, 22794, 22801, 22804, 22805, 22806, 22809, 22821,
5334
- 22849, 22852, 22853, 22854, 22857, 22864, 22865, 22866, 22868, 22869, 22870, 22872, 22873, 22874, 22881, 22884,
5335
- 22885, 22886, 22889, 22913, 22917, 22921, 22929, 22932, 22933, 22934, 22936, 22937, 22949, 23044, 23048, 23061,
5336
- 23066, 23072, 23077, 23078, 23081, 23109, 23112, 23113, 23121, 23125, 23126, 23128, 23129, 23138, 23141, 23144,
5337
- 23146, 23169, 23178, 23186, 23189, 23190, 23192, 23194, 23201, 24581, 24596, 24598, 24601, 24613, 24644, 24656,
5338
- 24661, 24662, 24664, 24666, 24673, 24676, 24678, 24681, 24705, 24726, 24741, 24833, 24836, 24838, 24841, 24850,
5339
- 24853, 24865, 24866, 24870, 24873, 24901, 24905, 24913, 24917, 24918, 24921, 24933, 24934, 24938, 24964, 24970,
5340
- 24978, 24981, 24993, 24998, 25001, 25105, 25110, 25113, 25152, 25153, 25158, 25173, 25174, 25176, 25184, 25221,
5341
- 25233, 25238, 25253, 25617, 25618, 25621, 25622, 25626, 25633, 25638, 25641, 25664, 25666, 25669, 25672, 25674,
5342
- 25681, 25684, 25685, 25686, 25689, 25690, 25696, 25698, 25701, 25732, 25733, 25737, 25744, 25746, 25748, 25749,
5343
- 25750, 25752, 25754, 25761, 25764, 25769, 25861, 25864, 25866, 25873, 25877, 25878, 25881, 25924, 25925, 25926,
5344
- 25929, 25936, 25937, 25940, 25941, 25942, 25945, 25953, 25956, 25957, 25958, 25961, 25990, 25993, 25994, 26001,
5345
- 26005, 26006, 26009, 26010, 26018, 26021, 26022, 26024, 26114, 26121, 26133, 26144, 26150, 26152, 26153, 26176,
5346
- 26181, 26184, 26186, 26193, 26196, 26197, 26198, 26200, 26202, 26208, 26213, 26216, 26240, 26242, 26245, 26250,
5347
- 26260, 26262, 26264, 26265, 26272, 26276, 26278, 26282, 26646, 26649, 26661, 26689, 26706, 26709, 26714, 26721,
5348
- 26729, 26757, 26769, 26776, 26790, 26881, 26884, 26896, 26901, 26913, 26916, 26918, 26921, 26944, 26945, 26949,
5349
- 26950, 26952, 26961, 26964, 26965, 26966, 26969, 26976, 26981, 26986, 27010, 27012, 27018, 27029, 27041, 27044,
5350
- 27045, 27049, 27153, 27158, 27160, 27201, 27204, 27209, 27216, 27221, 27224, 27226, 27236, 27237, 27241, 27270,
5351
- 27284, 27288, 27290, 27302, 32768, 32770, 32776, 32778, 32800, 32802, 32808, 32810, 32837, 32848, 32849, 32852,
5352
- 32854, 32857, 32869, 32896, 32898, 32904, 32906, 32917, 32928, 32930, 32936, 32938, 33029, 33041, 33044, 33046,
5353
- 33049, 33061, 33089, 33092, 33097, 33104, 33106, 33109, 33110, 33112, 33113, 33124, 33126, 33129, 33157, 33161,
5354
- 33172, 33174, 33177, 33189, 33280, 33282, 33288, 33290, 33301, 33312, 33314, 33320, 33322, 33361, 33364, 33369,
5355
- 33381, 33408, 33410, 33416, 33418, 33429, 33440, 33442, 33448, 33450, 33812, 33817, 33857, 33860, 33873, 33877,
5356
- 33882, 33889, 33892, 33897, 33940, 33945, 34049, 34057, 34066, 34069, 34074, 34086, 34089, 34112, 34113, 34117,
5357
- 34120, 34129, 34132, 34133, 34134, 34137, 34138, 34149, 34150, 34152, 34154, 34177, 34180, 34182, 34185, 34192,
5358
- 34194, 34197, 34200, 34214, 34321, 34326, 34329, 34341, 34369, 34372, 34377, 34378, 34384, 34389, 34393, 34394,
5359
- 34401, 34406, 34410, 34437, 34449, 34458, 34468, 34816, 34818, 34824, 34826, 34837, 34848, 34850, 34856, 34858,
5360
- 34881, 34885, 34897, 34900, 34905, 34917, 34921, 34944, 34946, 34952, 34954, 34965, 34976, 34978, 34984, 34986,
5361
- 35077, 35078, 35089, 35092, 35094, 35109, 35137, 35140, 35142, 35145, 35152, 35154, 35157, 35162, 35169, 35172,
5362
- 35205, 35222, 35225, 35237, 35328, 35330, 35336, 35338, 35349, 35360, 35362, 35368, 35370, 35397, 35409, 35412,
5363
- 35414, 35456, 35458, 35464, 35466, 35477, 35488, 35490, 35496, 35498, 36869, 36881, 36886, 36888, 36889, 36901,
5364
- 36929, 36934, 36937, 36949, 36952, 36954, 36969, 36970, 36997, 37009, 37012, 37014, 37017, 37029, 37121, 37124,
5365
- 37126, 37129, 37136, 37141, 37144, 37146, 37153, 37156, 37158, 37161, 37184, 37189, 37200, 37201, 37204, 37205,
5366
- 37206, 37209, 37218, 37221, 37252, 37254, 37266, 37269, 37272, 37281, 37284, 37286, 37289, 37381, 37393, 37396,
5367
- 37401, 37413, 37444, 37446, 37449, 37456, 37458, 37461, 37464, 37478, 37481, 37509, 37524, 37526, 37545, 37889,
5368
- 37892, 37894, 37904, 37909, 37912, 37926, 37952, 37962, 37969, 37972, 37973, 37974, 37976, 37977, 37984, 37985,
5369
- 37986, 37989, 38020, 38022, 38034, 38036, 38037, 38040, 38049, 38057, 38144, 38149, 38152, 38154, 38160, 38161,
5370
- 38164, 38165, 38166, 38169, 38177, 38181, 38185, 38186, 38209, 38212, 38213, 38214, 38217, 38224, 38225, 38226,
5371
- 38228, 38229, 38230, 38232, 38233, 38234, 38241, 38244, 38245, 38246, 38249, 38273, 38277, 38280, 38289, 38290,
5372
- 38292, 38293, 38294, 38297, 38298, 38304, 38306, 38309, 38312, 38314, 38401, 38404, 38416, 38421, 38425, 38432,
5373
- 38438, 38441, 38469, 38472, 38473, 38481, 38482, 38485, 38486, 38489, 38501, 38504, 38530, 38532, 38537, 38538,
5374
- 38546, 38548, 38549, 38564, 38566, 38569, 38917, 38934, 38937, 38949, 38977, 38982, 38992, 38994, 38997, 38998,
5375
- 39002, 39012, 39013, 39045, 39057, 39062, 39065, 39077, 39172, 39174, 39177, 39184, 39186, 39189, 39192, 39194,
5376
- 39200, 39201, 39204, 39206, 39232, 39234, 39237, 39240, 39242, 39249, 39252, 39253, 39254, 39257, 39266, 39269,
5377
- 39270, 39274, 39297, 39300, 39312, 39314, 39317, 39322, 39329, 39334, 39429, 39445, 39461, 39492, 39494, 39497,
5378
- 39504, 39509, 39512, 39521, 39557, 39569, 39572, 39573, 39574, 40960, 40962, 40968, 40970, 40981, 40992, 40994,
5379
- 41000, 41002, 41029, 41041, 41044, 41046, 41049, 41088, 41090, 41096, 41098, 41109, 41120, 41122, 41128, 41130,
5380
- 41221, 41225, 41233, 41236, 41238, 41241, 41242, 41286, 41289, 41297, 41301, 41304, 41306, 41313, 41316, 41349,
5381
- 41360, 41362, 41366, 41369, 41474, 41480, 41482, 41488, 41497, 41506, 41512, 41514, 41541, 41553, 41558, 41561,
5382
- 41573, 41600, 41602, 41608, 41610, 41621, 41632, 41634, 41640, 41642, 42009, 42021, 42049, 42052, 42064, 42068,
5383
- 42069, 42072, 42074, 42081, 42085, 42086, 42088, 42089, 42117, 42246, 42249, 42256, 42258, 42261, 42264, 42278,
5384
- 42281, 42306, 42309, 42321, 42324, 42325, 42326, 42329, 42341, 42346, 42369, 42372, 42373, 42374, 42377, 42386,
5385
- 42389, 42392, 42501, 42513, 42518, 42522, 42529, 42533, 42564, 42566, 42570, 42578, 42581, 42582, 42584, 42592,
5386
- 42594, 42630, 42640, 42645, 42646, 42649, 42657, 42660, 42662, 43008, 43010, 43016, 43018, 43040, 43042, 43048,
5387
- 43050, 43089, 43092, 43094, 43097, 43136, 43138, 43144, 43146, 43157, 43168, 43170, 43176, 43178, 43269, 43284,
5388
- 43289, 43297, 43301, 43329, 43344, 43349, 43354, 43361, 43366, 43369, 43408, 43414, 43520, 43522, 43528, 43530,
5389
- 43552, 43554, 43560, 43562, 43601, 43604, 43606, 43648, 43650, 43656, 43658, 43669, 43680, 43682, 43688, 43690,
5390
- };
5391
-
5392
- static uint64_t *hex_iq1s_grid = NULL;
5393
- static int *hex_iq1s_map = NULL;
5394
- static uint16_t *hex_iq1s_neighbours = NULL;
5395
- static uint64_t *hex_iq1s_dgrid = NULL; /* dequant grid: bytes in {-1,0,+1} */
5396
- static int hex_iq1s_ready = 0;
5397
-
5398
- static int hex_iq1s_cmp(const void *left, const void *right) {
5399
- const int *l = (const int *)left;
5400
- const int *r = (const int *)right;
5401
- if (l[0] != r[0]) return l[0] < r[0] ? -1 : 1;
5402
- if (l[1] != r[1]) return l[1] < r[1] ? -1 : 1;
5403
- return 0;
5404
- }
5405
-
5406
- typedef struct { float v; int i; } hex_sort_pair;
5407
- static int hex_iq1s_sort_helper(const void *left, const void *right) {
5408
- const hex_sort_pair *l = (const hex_sort_pair *)left;
5409
- const hex_sort_pair *r = (const hex_sort_pair *)right;
5410
- if (l->v < r->v) return -1;
5411
- if (l->v > r->v) return 1;
5412
- return 0;
5413
- }
5414
-
5415
- static int hex_iq1s_best_neighbour(const uint16_t *neighbours, const uint64_t *grid,
5416
- const float *xval, const float *weight, float scale, const float *xg,
5417
- int8_t *L, int ngrid) {
5418
- int num_neighbors = neighbours[0];
5419
- float best_score = 1e30f;
5420
- int grid_index = -1;
5421
- int j, i;
5422
- if (num_neighbors > 0) {
5423
- for (j = 1; j <= num_neighbors; ++j) {
5424
- const int8_t *pg = (const int8_t *)(grid + neighbours[j]);
5425
- float d2 = 0;
5426
- for (i = 0; i < 8; ++i) {
5427
- float q = xg[(pg[i] - 1)/2];
5428
- float diff = scale*q - xval[i];
5429
- d2 += weight[i]*diff*diff;
5430
- }
5431
- if (d2 < best_score) { best_score = d2; grid_index = neighbours[j]; }
5432
- }
5433
- }
5434
- if (grid_index < 0) {
5435
- for (i = 0; i < ngrid; ++i) {
5436
- const int8_t *gi = (const int8_t *)(grid + i);
5437
- float d2 = 0;
5438
- for (j = 0; j < 8; ++j) {
5439
- float q = xg[(gi[j] - 1)/2];
5440
- float diff = scale*q - xval[j];
5441
- d2 += weight[j]*diff*diff;
5442
- }
5443
- if (d2 < best_score) { best_score = d2; grid_index = i; }
5444
- }
5445
- }
5446
- if (grid_index < 0) return -1;
5447
- {
5448
- const int8_t *pg = (const int8_t *)(grid + grid_index);
5449
- for (i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
5450
- }
5451
- return grid_index;
5452
- }
5453
-
5454
- /* One-time build of grid + kmap + neighbours (ggml iq2xs_init_impl, IQ1_S branch). */
5455
- static void hex_iq1s_init(void) {
5456
- int k, i, j;
5457
- if (hex_iq1s_ready) return;
5458
- hex_iq1s_ready = 1;
5459
-
5460
- uint64_t *the_grid = (uint64_t *)malloc(HEX_NGRID_IQ1S*sizeof(uint64_t));
5461
- if (!the_grid) return;
5462
- for (k = 0; k < HEX_NGRID_IQ1S; ++k) {
5463
- int8_t *pos = (int8_t *)(the_grid + k);
5464
- for (i = 0; i < 8; ++i) {
5465
- int l = (hex_kgrid_1bit_2048[k] >> 2*i) & 0x3;
5466
- pos[i] = (int8_t)(2*l + 1);
5467
- }
5468
- }
5469
- hex_iq1s_grid = the_grid;
5470
-
5471
- /* Dequant grid: same patterns mapped to {-1,0,+1} (matches ggml iq1s_grid). */
5472
- {
5473
- uint64_t *dg = (uint64_t *)malloc(HEX_NGRID_IQ1S*sizeof(uint64_t));
5474
- if (dg) {
5475
- for (k = 0; k < HEX_NGRID_IQ1S; ++k) {
5476
- int8_t *pos = (int8_t *)(dg + k);
5477
- for (i = 0; i < 8; ++i) {
5478
- int l = (hex_kgrid_1bit_2048[k] >> 2*i) & 0x3;
5479
- pos[i] = (int8_t)(l - 1);
5480
- }
5481
- }
5482
- hex_iq1s_dgrid = dg;
5483
- }
5484
- }
5485
-
5486
- int *kmap = (int *)malloc(HEX_KMAP_IQ1S_SIZE*sizeof(int));
5487
- if (!kmap) return;
5488
- for (i = 0; i < HEX_KMAP_IQ1S_SIZE; ++i) kmap[i] = -1;
5489
- {
5490
- uint64_t aux64;
5491
- uint8_t *aux8 = (uint8_t *)&aux64;
5492
- for (i = 0; i < HEX_NGRID_IQ1S; ++i) {
5493
- aux64 = the_grid[i];
5494
- uint16_t index = 0;
5495
- for (k = 0; k < 8; ++k) {
5496
- uint16_t q = (uint16_t)((aux8[k] - 1)/2);
5497
- index |= (uint16_t)(q << 2*k);
5498
- }
5499
- kmap[index] = i;
5500
- }
5501
- }
5502
- hex_iq1s_map = kmap;
5503
-
5504
- {
5505
- int *n_per_i = (int *)malloc(HEX_KMAP_IQ1S_SIZE*sizeof(int));
5506
- int *offsets = NULL;
5507
- uint16_t *neighbours = NULL;
5508
- int num_neighbors = 0, num_not_in_map = 0;
5509
- int counter = 0;
5510
- if (!n_per_i) return;
5511
- #pragma omp parallel reduction(+:num_neighbors,num_not_in_map)
5512
- {
5513
- int *dist2 = (int *)malloc(2*HEX_NGRID_IQ1S*sizeof(int));
5514
- int8_t pos[8];
5515
- int ii;
5516
- if (dist2) {
5517
- #pragma omp for schedule(dynamic, 64)
5518
- for (ii = 0; ii < HEX_KMAP_IQ1S_SIZE; ++ii) {
5519
- int a, b;
5520
- if (kmap[ii] >= 0) { n_per_i[ii] = 0; continue; }
5521
- ++num_not_in_map;
5522
- for (k = 0; k < 8; ++k) pos[k] = (int8_t)(2*((ii >> 2*k) & 0x3) + 1);
5523
- for (j = 0; j < HEX_NGRID_IQ1S; ++j) {
5524
- const int8_t *pg = (const int8_t *)(the_grid + j);
5525
- int d2 = 0;
5526
- for (k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
5527
- dist2[2*j+0] = d2; dist2[2*j+1] = j;
5528
- }
5529
- qsort(dist2, HEX_NGRID_IQ1S, 2*sizeof(int), hex_iq1s_cmp);
5530
- {
5531
- int n = 0, d2 = dist2[0], nhave = 1;
5532
- for (a = 0; a < HEX_NGRID_IQ1S; ++a) {
5533
- if (dist2[2*a] > d2) {
5534
- if (nhave == 3) break;
5535
- d2 = dist2[2*a]; ++nhave;
5536
- }
5537
- ++n;
5538
- }
5539
- n_per_i[ii] = n;
5540
- num_neighbors += n;
5541
- }
5542
- }
5543
- free(dist2);
5544
- }
5545
- }
5546
- neighbours = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
5547
- offsets = (int *)malloc(HEX_KMAP_IQ1S_SIZE*sizeof(int));
5548
- if (!neighbours || !offsets) { free(neighbours); free(offsets); free(n_per_i); return; }
5549
- for (i = 0; i < HEX_KMAP_IQ1S_SIZE; ++i) {
5550
- if (kmap[i] >= 0) { offsets[i] = -1; continue; }
5551
- offsets[i] = counter;
5552
- counter += 1 + n_per_i[i];
5553
- }
5554
- #pragma omp parallel
5555
- {
5556
- int *dist2 = (int *)malloc(2*HEX_NGRID_IQ1S*sizeof(int));
5557
- int8_t pos[8];
5558
- int ii;
5559
- if (dist2) {
5560
- #pragma omp for schedule(dynamic, 64)
5561
- for (ii = 0; ii < HEX_KMAP_IQ1S_SIZE; ++ii) {
5562
- int a;
5563
- uint16_t *start;
5564
- int local_counter, d2, n = 0, nhave = 1;
5565
- if (kmap[ii] >= 0) continue;
5566
- for (k = 0; k < 8; ++k) pos[k] = (int8_t)(2*((ii >> 2*k) & 0x3) + 1);
5567
- for (j = 0; j < HEX_NGRID_IQ1S; ++j) {
5568
- const int8_t *pg = (const int8_t *)(the_grid + j);
5569
- int dd = 0;
5570
- for (k = 0; k < 8; ++k) dd += (pg[k] - pos[k])*(pg[k] - pos[k]);
5571
- dist2[2*j+0] = dd; dist2[2*j+1] = j;
5572
- }
5573
- qsort(dist2, HEX_NGRID_IQ1S, 2*sizeof(int), hex_iq1s_cmp);
5574
- local_counter = offsets[ii];
5575
- kmap[ii] = -(local_counter + 1);
5576
- d2 = dist2[0];
5577
- start = &neighbours[local_counter++];
5578
- for (a = 0; a < HEX_NGRID_IQ1S; ++a) {
5579
- if (dist2[2*a] > d2) {
5580
- if (nhave == 3) break;
5581
- d2 = dist2[2*a]; ++nhave;
5582
- }
5583
- neighbours[local_counter++] = (uint16_t)dist2[2*a+1];
5584
- ++n;
5585
- }
5586
- *start = (uint16_t)n;
5587
- }
5588
- free(dist2);
5589
- }
5590
- }
5591
- free(offsets);
5592
- free(n_per_i);
5593
- hex_iq1s_neighbours = neighbours;
5594
- }
5595
- }
5596
-
5597
- static int hex_nearest_int(float fval) {
5598
- float v = fval;
5599
- if (!(v > -4194303.f && v < 4194303.f)) v = v < 0 ? -4194303.f : 4194303.f;
5600
- {
5601
- float val = v + 12582912.f;
5602
- int i;
5603
- memcpy(&i, &val, sizeof(int));
5604
- return (i & 0x007fffff) - 0x00400000;
5605
- }
5606
- }
5607
-
5608
- static void hex_iq1s_dequant_block(const BlockIQ1S *b, float *out) {
5609
- float d = gguf_fp16_to_fp32(b->d);
5610
- int ib, l, j;
5611
- const uint8_t *qs = b->qs;
5612
- for (ib = 0; ib < QK_K/32; ++ib) {
5613
- uint16_t h = b->qh[ib];
5614
- float dl = d * (2*((h >> 12) & 7) + 1);
5615
- float delta = (h & 0x8000) ? -HEX_IQ1S_DELTA : HEX_IQ1S_DELTA;
5616
- for (l = 0; l < 4; ++l) {
5617
- unsigned gi = (unsigned)qs[l] | (unsigned)(((h >> (3*l)) & 7) << 8);
5618
- const int8_t *grid = (const int8_t *)(hex_iq1s_dgrid + gi);
5619
- for (j = 0; j < 8; ++j) out[j] = dl * (grid[j] + delta);
5620
- out += 8;
5621
- }
5622
- qs += 4;
5623
- }
5624
- }
5625
-
5626
- /* HPC IQ1_S tensor quantizer: mirrors quantize_tensor_q2k_hpc front-end
5627
- * (kurtosis-driven clamp, OpenMP, imatrix, MSE) with IQ1_S geometry. */
5628
-
5629
- /* ---- IQ1_S sieve/triality/vesica selection stage ----
5630
- * Geometry stays ggml-compatible (packing untouched); the SELECTION of
5631
- * per-sub-block configuration runs the same machinery as Q2_K/Q4_0/Q8_0:
5632
- * Boltzmann-encoded candidate errors -> triality mask -> CZ chain ->
5633
- * sieve_measure_graph -> in-bin selection with greedy override, then
5634
- * ULP polish accepted under the vesica/DC extended metric. */
5635
- #define HEX_IQ1S_NCAND 6
5636
- #define HEX_IQ1S_GROUP_BLKS 64
5637
- static const int HEX_IQ1S_CAND_TO_QUHIT[6] = {0, 1, 2, 3, 4, 5};
5638
-
5639
- /* Evaluate one (boundaries, shift) candidate: L codes, WLS scale,
5640
- * grid indices, extended error (wSSE + spectral). */
5641
- static float hex_iq1s_eval_cand(const float *xb, const float *weight,
5642
- const int *order, int i1, int i2, int shift,
5643
- const float *xxp, const float *xxm,
5644
- int8_t *Lout, uint16_t *idxout, float *scaleout)
5645
- {
5646
- const float *xx = shift == 1 ? xxp : xxm;
5647
- int L[32];
5648
- float sumqx = 0.0f, sumq2 = 0.0f;
5649
- float scale;
5650
- float e[32];
5651
- int r, j, k;
5652
- if (i1 < 0) i1 = 0; if (i1 > 32) i1 = 32;
5653
- if (i2 < 0) i2 = 0; if (i2 > 32) i2 = 32;
5654
- if (i1 > i2) { int t = i1; i1 = i2; i2 = t; }
5655
- for (r = 0; r < i1; ++r) L[order[r]] = 0;
5656
- for (r = i1; r < i2; ++r) L[order[r]] = 1;
5657
- for (r = i2; r < 32; ++r) L[order[r]] = 2;
5658
- for (j = 0; j < 32; ++j) {
5659
- float q = xx[L[j]];
5660
- sumqx += weight[j]*q*xb[j];
5661
- sumq2 += weight[j]*q*q;
5662
- }
5663
- if (!(sumq2 > 0.0f)) return 1e30f;
5664
- scale = sumqx/sumq2;
5665
- if (scale < 0.0f) {
5666
- for (j = 0; j < 32; ++j) L[j] = 2 - L[j];
5667
- scale = -scale;
5668
- }
5669
- for (k = 0; k < 4; ++k) {
5670
- uint16_t u = 0;
5671
- for (j = 0; j < 8; ++j) u |= (uint16_t)(L[8*k+j] << 2*j);
5672
- {
5673
- int gi = hex_iq1s_map[u];
5674
- if (gi < 0) {
5675
- const uint16_t *neighbours = hex_iq1s_neighbours - hex_iq1s_map[u] - 1;
5676
- gi = hex_iq1s_best_neighbour(neighbours, hex_iq1s_grid,
5677
- xb + 8*k, weight + 8*k, scale, xx, (int8_t *)(L + 8*k), HEX_NGRID_IQ1S);
5678
- if (gi < 0) return 1e30f;
5679
- }
5680
- idxout[k] = (uint16_t)gi;
5681
- }
5682
- }
5683
- /* reconstruct with final codes for scoring */
5684
- {
5685
- float wsum = 0.0f;
5686
- for (j = 0; j < 32; ++j) {
5687
- int grp = j / 8, pos = j % 8;
5688
- const int8_t *pg = (const int8_t *)(hex_iq1s_grid + idxout[grp]);
5689
- int code = (pg[pos] - 1)/2;
5690
- if (code < 0) code = 0; if (code > 2) code = 2;
5691
- e[j] = xb[j] - scale*xx[code];
5692
- wsum += weight[j]*e[j]*e[j];
5693
- }
5694
- wsum += hex_spectral_penalty(e, 32);
5695
- for (j = 0; j < 32; ++j) Lout[j] = (int8_t)L[j];
5696
- *scaleout = scale;
5697
- return wsum;
5698
- }
5699
- }
5700
-
5701
- /* Extended block metric for ULP accept: wSSE + spectral + vesica/DC. */
5702
- static float hex_iq1s_block_metric(const float *xb, const float *deq, const float *w)
5703
- {
5704
- float e[256];
5705
- float sse = 0.0f, ves = 0.0f, dc = 0.0f;
5706
- int j;
5707
- for (j = 0; j < 256; ++j) {
5708
- e[j] = xb[j] - deq[j];
5709
- sse += (w ? w[j] : 1.0f)*e[j]*e[j];
5710
- dc += e[j];
5711
- }
5712
- for (j = 0; j < 128; ++j) {
5713
- float v = e[j] + e[j+128];
5714
- ves += v*v;
5715
- }
5716
- return sse + hex_spectral_penalty(e, 256) + (4.0f*ves + dc*dc)/256.0f;
5717
- }
5718
- static void quantize_tensor_iq1_s_hpc(const float *weights, int64_t n_elements,
5719
- BlockIQ1S *output, float *out_total_error,
5720
- const float *imat_importance,
5721
- int verbose)
5722
-
5723
- {
5724
- int64_t n_blocks;
5725
- float total_err = 0.0f;
5726
- double t_sum_sq = 0.0, t_sum_4 = 0.0;
5727
- double t_var, t_kurt;
5728
- float w_sigma, clamp_sigma, clamp_val;
5729
- int64_t i, gbase;
5730
- int SMAX = HEX_IQ1S_GROUP_BLKS*8;
5731
- int NC = HEX_IQ1S_NCAND;
5732
- int plain = 0; /* HEX_IQ1S_PLAIN=1: ggml-exact, no sieve/ULP (A/B reference) */
5733
- float *g_err = NULL;
5734
- int8_t *g_i1 = NULL, *g_i2 = NULL, *g_sh = NULL;
5735
- float *g_sc = NULL;
5736
- uint16_t *g_gi = NULL;
5737
- int *g_sel = NULL;
5738
-
5739
- if (!weights || !output || n_elements <= 0 || n_elements % QK_K != 0) {
5740
- if (out_total_error) *out_total_error = -1.0f;
5741
- return;
5742
- }
5743
- hex_iq1s_init();
5744
- if (!hex_iq1s_grid || !hex_iq1s_map || !hex_iq1s_neighbours || !hex_iq1s_dgrid) {
5745
- if (out_total_error) *out_total_error = -1.0f;
5746
- return;
5747
- }
5748
- n_blocks = n_elements / QK_K;
5749
- {
5750
- const char *penv = getenv("HEX_IQ1S_PLAIN");
5751
- if (penv && penv[0] == '1') plain = 1;
5752
- }
5753
-
5754
- for (i = 0; i < n_elements; i++) {
5755
- double w2 = (double)weights[i] * (double)weights[i];
5756
- t_sum_sq += w2;
5757
- t_sum_4 += w2 * w2;
5758
- }
5759
- w_sigma = sqrtf((float)(t_sum_sq / (double)n_elements));
5760
- t_var = t_sum_sq / (double)n_elements;
5761
- t_kurt = (t_var > 1e-30) ? (t_sum_4 / (double)n_elements) / (t_var * t_var) : 3.0;
5762
- if (t_kurt <= 6.0) clamp_sigma = 1.0e9f;
5763
- else if (t_kurt <= 20.0) clamp_sigma = 6.0f;
5764
- else clamp_sigma = 4.0f;
5765
- clamp_val = w_sigma * clamp_sigma;
5766
- if (verbose) {
5767
- char kl[128];
5768
- sprintf(kl, " [iq1_s-hpc] kurt=%.2f clamp=%.3g sigma=%.6g sieve=on", t_kurt, (double)clamp_val, (double)w_sigma);
5769
- puts(kl);
5770
- }
5771
-
5772
- g_err = (float *)malloc((size_t)SMAX*NC*sizeof(float));
5773
- g_i1 = (int8_t *)malloc((size_t)SMAX*NC);
5774
- g_i2 = (int8_t *)malloc((size_t)SMAX*NC);
5775
- g_sh = (int8_t *)malloc((size_t)SMAX*NC);
5776
- g_sc = (float *)malloc((size_t)SMAX*NC*sizeof(float));
5777
- g_gi = (uint16_t *)malloc((size_t)SMAX*NC*4*sizeof(uint16_t));
5778
- g_sel = (int *)malloc((size_t)SMAX*sizeof(int));
5779
- if (!g_err || !g_i1 || !g_i2 || !g_sh || !g_sc || !g_gi || !g_sel) {
5780
- free(g_err); free(g_i1); free(g_i2); free(g_sh); free(g_sc); free(g_gi); free(g_sel);
5781
- if (out_total_error) *out_total_error = -1.0f;
5782
- return;
5783
- }
5784
-
5785
- for (gbase = 0; gbase < n_blocks; gbase += HEX_IQ1S_GROUP_BLKS) {
5786
- int64_t gb = n_blocks - gbase;
5787
- int ns, u, s, c;
5788
- HPCGraph *graph = NULL;
5789
- double (*marg)[6] = NULL;
5790
- int *measured = NULL;
5791
- int shor_ran = 0;
5792
- float temperature = 0.5f;
5793
- if (gb > HEX_IQ1S_GROUP_BLKS) gb = HEX_IQ1S_GROUP_BLKS;
5794
- ns = (int)(gb*8);
5795
-
5796
- /* Phase 1+2: greedy seeds + sieve candidates (parallel over sub-blocks) */
5797
- #pragma omp parallel for schedule(dynamic, 8)
5798
- for (u = 0; u < ns; ++u) {
5799
- int64_t blk = gbase + u/8;
5800
- int ib = u%8;
5801
- const float *xbl = weights + blk*QK_K;
5802
- const float *qwb = imat_importance ? imat_importance + blk*QK_K : NULL;
5803
- const float *xb = xbl + 32*ib;
5804
- float qw[32], weight[32], xc[32];
5805
- hex_sort_pair pairs[32];
5806
- int order[32];
5807
- float sumx[33], sumw[33];
5808
- float xp[3], xm[3];
5809
- float best_scale, maxv;
5810
- int besti1, besti2, best_shift;
5811
- int i1, i2, j;
5812
- int ci1[6], ci2[6], csh[6];
5813
- float qwo[32];
5814
- if (!qwb) {
5815
- for (j = 0; j < 32; ++j) qwo[j] = 1.0f;
5816
- qwb = qwo;
5817
- }
5818
- for (j = 0; j < 32; ++j) {
5819
- qw[j] = qwb[j];
5820
- weight[j] = qw[j] * sqrtf(1.0f + xb[j]*xb[j]);
5821
- }
5822
- maxv = fabsf(xb[0]);
5823
- for (j = 1; j < 32; ++j) { float a = fabsf(xb[j]); if (a > maxv) maxv = a; }
5824
- if (!(maxv >= HEX_GROUP_MAX_EPS_IQ1_S)) {
5825
- for (c = 0; c < NC; ++c) {
5826
- g_err[u*NC+c] = 1e30f;
5827
- g_i1[u*NC+c] = 0; g_i2[u*NC+c] = 0; g_sh[u*NC+c] = 1;
5828
- g_sc[u*NC+c] = 0.0f;
5829
- g_gi[(u*NC+c)*4+0] = 0; g_gi[(u*NC+c)*4+1] = 0;
5830
- g_gi[(u*NC+c)*4+2] = 0; g_gi[(u*NC+c)*4+3] = 0;
5831
- }
5832
- continue;
5833
- }
5834
- {
5835
- float sumx2 = 0.0f;
5836
- for (j = 0; j < QK_K; ++j) sumx2 += xbl[j]*xbl[j];
5837
- {
5838
- float s2 = 2*sumx2/QK_K;
5839
- for (j = 0; j < 32; ++j) weight[j] = qw[j] * sqrtf(s2 + xb[j]*xb[j]);
5840
- }
5841
- }
5842
- for (j = 0; j < 32; ++j) {
5843
- float v = xb[j];
5844
- if (v > clamp_val) v = clamp_val;
5845
- if (v < -clamp_val) v = -clamp_val;
5846
- xc[j] = v;
5847
- pairs[j].v = v; pairs[j].i = j;
5848
- }
5849
- qsort(pairs, 32, sizeof(hex_sort_pair), hex_iq1s_sort_helper);
5850
- for (j = 0; j < 32; ++j) order[j] = pairs[j].i;
5851
- sumx[0] = sumw[0] = 0.0f;
5852
- for (j = 0; j < 32; ++j) {
5853
- int gi2 = order[j];
5854
- sumx[j+1] = sumx[j] + weight[gi2]*xc[gi2];
5855
- sumw[j+1] = sumw[j] + weight[gi2];
5856
- }
5857
- xp[0] = -1 + HEX_IQ1S_DELTA; xp[1] = HEX_IQ1S_DELTA; xp[2] = 1 + HEX_IQ1S_DELTA;
5858
- xm[0] = -1 - HEX_IQ1S_DELTA; xm[1] = -HEX_IQ1S_DELTA; xm[2] = 1 - HEX_IQ1S_DELTA;
5859
- {
5860
- float bsc = -1e30f;
5861
- best_scale = maxv; besti1 = 0; besti2 = 0; best_shift = 1;
5862
- for (i1 = 0; i1 <= 32; ++i1) {
5863
- for (i2 = i1; i2 <= 32; ++i2) {
5864
- float sumqx = (sumx[i1]-sumx[0])*xp[0] + (sumx[i2]-sumx[i1])*xp[1] + (sumx[32]-sumx[i2])*xp[2];
5865
- float sumq2 = (sumw[i1]-sumw[0])*xp[0]*xp[0] + (sumw[i2]-sumw[i1])*xp[1]*xp[1] + (sumw[32]-sumw[i2])*xp[2]*xp[2];
5866
- if (sumq2 > 0 && sumqx*sumqx > bsc*sumq2) {
5867
- best_scale = sumqx/sumq2; bsc = best_scale*sumqx;
5868
- besti1 = i1; besti2 = i2; best_shift = 1;
5869
- }
5870
- sumqx = (sumx[i1]-sumx[0])*xm[0] + (sumx[i2]-sumx[i1])*xm[1] + (sumx[32]-sumx[i2])*xm[2];
5871
- sumq2 = (sumw[i1]-sumw[0])*xm[0]*xm[0] + (sumw[i2]-sumw[i1])*xm[1]*xm[1] + (sumw[32]-sumw[i2])*xm[2]*xm[2];
5872
- if (sumq2 > 0 && sumqx*sumqx > bsc*sumq2) {
5873
- best_scale = sumqx/sumq2; bsc = best_scale*sumqx;
5874
- besti1 = i1; besti2 = i2; best_shift = -1;
5875
- }
5876
- }
5877
- }
5878
- }
5879
- ci1[0] = besti1; ci2[0] = besti2; csh[0] = best_shift;
5880
- ci1[1] = besti1-1; ci2[1] = besti2; csh[1] = best_shift;
5881
- ci1[2] = besti1+1; ci2[2] = besti2; csh[2] = best_shift;
5882
- ci1[3] = besti1; ci2[3] = besti2-1; csh[3] = best_shift;
5883
- ci1[4] = besti1; ci2[4] = besti2+1; csh[4] = best_shift;
5884
- ci1[5] = besti1; ci2[5] = besti2; csh[5] = -best_shift;
5885
- for (c = 0; c < NC; ++c) {
5886
- int8_t Lc[32];
5887
- uint16_t ix[4];
5888
- float sc = 0.0f;
5889
- float e = hex_iq1s_eval_cand(xb, weight, order, ci1[c], ci2[c], csh[c],
5890
- xp, xm, Lc, ix, &sc);
5891
- g_err[u*NC+c] = e;
5892
- g_i1[u*NC+c] = (int8_t)(ci1[c] < 0 ? 0 : ci1[c] > 32 ? 32 : ci1[c]);
5893
- g_i2[u*NC+c] = (int8_t)(ci2[c] < 0 ? 0 : ci2[c] > 32 ? 32 : ci2[c]);
5894
- g_sh[u*NC+c] = (int8_t)csh[c];
5895
- g_sc[u*NC+c] = sc;
5896
- g_gi[(u*NC+c)*4+0] = ix[0]; g_gi[(u*NC+c)*4+1] = ix[1];
5897
- g_gi[(u*NC+c)*4+2] = ix[2]; g_gi[(u*NC+c)*4+3] = ix[3];
5898
- }
5899
- }
5900
-
5901
- /* Phase 3: triality encode + CZ chain + sieve measurement (mirrors Q4) */
5902
- graph = hpc_create((uint64_t)ns);
5903
- if (graph) {
5904
- {
5905
- double eacc = 0.0;
5906
- int ecount = 0;
5907
- for (s = 0; s < ns && s < 100; ++s) {
5908
- float mx = 0.0f;
5909
- for (c = 0; c < NC; ++c)
5910
- if (g_err[s*NC+c] > mx) mx = g_err[s*NC+c];
5911
- eacc += (double)mx; ecount++;
5912
- }
5913
- if (ecount > 0) {
5914
- temperature = (float)(eacc / ecount) * 0.1f;
5915
- if (temperature < 1e-10f) temperature = 1e-10f;
5916
- }
5917
- }
5918
- for (s = 0; s < ns; ++s) triality_dft(&graph->locals[s]);
5919
- for (s = 0; s < ns; ++s) {
5920
- float mn = 1e30f;
5921
- double amp_re[6], amp_norm = 0.0;
5922
- int v;
5923
- for (c = 0; c < NC; ++c)
5924
- if (g_err[s*NC+c] < mn) mn = g_err[s*NC+c];
5925
- for (v = 0; v < 6; ++v) amp_re[v] = 0.0;
5926
- for (c = 0; c < NC; ++c) {
5927
- int qi = HEX_IQ1S_CAND_TO_QUHIT[c];
5928
- amp_re[qi] += exp(-(double)(g_err[s*NC+c] - mn) / (2.0*(double)temperature));
5929
- }
5930
- for (v = 0; v < 6; ++v) amp_norm += amp_re[v]*amp_re[v];
5931
- if (amp_norm > 1e-30) {
5932
- double inv = 1.0/sqrt(amp_norm);
5933
- for (v = 0; v < 6; ++v) amp_re[v] *= inv;
5934
- }
5935
- for (v = 0; v < 6; ++v) {
5936
- graph->locals[s].edge_re[v] = amp_re[v];
5937
- graph->locals[s].edge_im[v] = 0.0;
5938
- }
5939
- graph->locals[s].primary = VIEW_EDGE;
5940
- graph->locals[s].dirty = DIRTY_VERTEX | DIRTY_DIAGONAL | DIRTY_FOLDED;
5941
- graph->locals[s].delta_valid = 0;
5942
- triality_update_mask(&graph->locals[s]);
5943
- }
5944
- for (s = 0; s < ns - 1; ++s) hpc_cz(graph, (uint64_t)s, (uint64_t)(s+1));
5945
- marg = (double (*)[6])calloc((size_t)ns, sizeof(double[6]));
5946
- measured = (int *)calloc((size_t)ns, sizeof(int));
5947
- if (marg && measured) {
5948
- sieve_measure_graph(graph, (int64_t)ns, marg, measured, 1);
5949
- for (s = 0; s < ns; ++s) {
5950
- int bin = measured[s];
5951
- int bb = 0, gb2 = 0;
5952
- float be = 1e30f, ge = 1e30f;
5953
- int bc = -1, gc = 0;
5954
- if (bin < 0 || bin > 5) {
5955
- double bm = -1.0;
5956
- bin = 0;
5957
- for (int v = 0; v < 6; ++v)
5958
- if (marg[s][v] > bm) { bm = marg[s][v]; bin = v; }
5959
- }
5960
- for (c = 0; c < NC; ++c) {
5961
- float e = g_err[s*NC+c];
5962
- if (e < ge) { ge = e; gc = c; }
5963
- if (HEX_IQ1S_CAND_TO_QUHIT[c] == bin && e < be) { be = e; bc = c; }
5964
- }
5965
- {
5966
- int sel = (bc >= 0) ? bc : gc;
5967
- if (ge < g_err[s*NC+sel] * HEX_GREEDY_OVERRIDE_RATIO) sel = gc;
5968
- g_sel[s] = sel;
5969
- }
5970
- (void)bb; (void)gb2;
5971
- }
5972
- shor_ran = 1;
5973
- }
5974
- free(marg); free(measured);
5975
- hpc_destroy(graph);
5976
- }
5977
- if (!shor_ran) {
5978
- for (s = 0; s < ns; ++s) {
5979
- float gb2 = g_err[s*NC+0];
5980
- int gc = 0;
5981
- for (c = 1; c < NC; ++c)
5982
- if (g_err[s*NC+c] < gb2) { gb2 = g_err[s*NC+c]; gc = c; }
5983
- g_sel[s] = gc;
5984
- }
5985
- }
5986
- if (plain) {
5987
- /* ggml-exact reference: exhaustive optimum only, no sieve. */
5988
- for (s = 0; s < ns; ++s) g_sel[s] = 0;
5989
- }
5990
-
5991
- /* Phase 4: pack winners + ULP polish under vesica guard + MSE */
5992
- #pragma omp parallel for schedule(dynamic, 8) reduction(+:total_err)
5993
- for (int64_t b = 0; b < gb; ++b) {
5994
- int64_t blk = gbase + b;
5995
- const float *xbl = weights + blk*QK_K;
5996
- const float *qwb = imat_importance ? imat_importance + blk*QK_K : NULL;
5997
- BlockIQ1S *yb = &output[blk];
5998
- float scales[8];
5999
- int8_t shifts[8];
6000
- float max_scale = 0.0f;
6001
- uint16_t qh_base[8];
6002
- float d, id;
6003
- uint16_t d16;
6004
- BlockIQ1S tmp;
6005
- float deq[256];
6006
- float best_metric;
6007
- int ib, ii, k, du;
6008
- float qw1[256];
6009
- const float *ww = qwb;
6010
- yb->d = 0;
6011
- memset(yb->qs, 0, QK_K/8);
6012
- memset(yb->qh, 0, QK_K/16);
6013
- if (!ww) {
6014
- for (ii = 0; ii < 256; ++ii) qw1[ii] = 1.0f;
6015
- ww = qw1;
6016
- }
6017
- for (ib = 0; ib < 8; ++ib) {
6018
- int sel = g_sel[b*8+ib];
6019
- scales[ib] = g_sc[(b*8+ib)*NC+sel];
6020
- shifts[ib] = g_sh[(b*8+ib)*NC+sel];
6021
- if (scales[ib] > max_scale) max_scale = scales[ib];
6022
- {
6023
- uint16_t h = 0;
6024
- for (k = 0; k < 4; ++k) {
6025
- uint16_t gx = g_gi[((b*8+ib)*NC+sel)*4+k];
6026
- yb->qs[4*ib+k] = (uint8_t)(gx & 255);
6027
- h |= (uint16_t)((gx >> 8) << 3*k);
6028
- }
6029
- qh_base[ib] = h;
6030
- yb->qh[ib] = h;
6031
- }
6032
- }
6033
- if (!(max_scale > 0.0f)) continue;
6034
- d = max_scale/15.0f;
6035
- d16 = gguf_fp32_to_fp16(d*1.125f);
6036
- yb->d = d16;
6037
- id = 1.0f/d;
6038
- for (ib = 0; ib < 8; ++ib) {
6039
- int l = hex_nearest_int(0.5f*(id*scales[ib]-1.0f));
6040
- if (l < 0) l = 0; if (l > 7) l = 7;
6041
- if (shifts[ib] == -1) l |= 8;
6042
- yb->qh[ib] |= (uint16_t)(l << 12);
6043
- }
6044
- memcpy(&tmp, yb, sizeof(BlockIQ1S));
6045
- hex_iq1s_dequant_block(&tmp, deq);
6046
- best_metric = hex_iq1s_block_metric(xbl, deq, ww);
6047
- if (!plain) {
6048
- for (du = -8; du <= 8; ++du) {
6049
- int c16;
6050
- float td;
6051
- float tid;
6052
- float tdeq[256];
6053
- float m;
6054
- if (du == 0) continue;
6055
- c16 = (int)d16 + du;
6056
- if (c16 <= 0 || c16 > 0x7BFF) continue;
6057
- td = gguf_fp16_to_fp32((uint16_t)c16);
6058
- if (!(td > 0.0f)) continue;
6059
- memcpy(&tmp, yb, sizeof(BlockIQ1S));
6060
- tmp.d = (uint16_t)c16;
6061
- tid = 1.0f/(td/1.125f);
6062
- for (ib = 0; ib < 8; ++ib) {
6063
- int l = hex_nearest_int(0.5f*(tid*scales[ib]-1.0f));
6064
- if (l < 0) l = 0; if (l > 7) l = 7;
6065
- if (shifts[ib] == -1) l |= 8;
6066
- tmp.qh[ib] = (uint16_t)(qh_base[ib] | (l << 12));
6067
- }
6068
- hex_iq1s_dequant_block(&tmp, tdeq);
6069
- m = hex_iq1s_block_metric(xbl, tdeq, ww);
6070
- if (m < best_metric) {
6071
- best_metric = m;
6072
- memcpy(yb, &tmp, sizeof(BlockIQ1S));
6073
- memcpy(deq, tdeq, sizeof(deq));
6074
- }
6075
- }
6076
- } /* end ULP polish (skipped in plain mode) */
6077
- for (ii = 0; ii < 256; ++ii) {
6078
- float e = xbl[ii] - deq[ii];
6079
- total_err += e*e;
6080
- }
6081
- }
6082
- }
6083
-
6084
- free(g_err); free(g_i1); free(g_i2); free(g_sh); free(g_sc); free(g_gi); free(g_sel);
6085
- if (out_total_error) *out_total_error = total_err;
6086
- }
6087
-
6088
-
6089
  void hexstate_init(void)
6090
  {
6091
  static int initialized = 0;
@@ -6169,50 +5322,6 @@ void hexstate_quantize_tensor_q8_0_hpc(const float *weights, int64_t n_elements,
6169
  imat_importance, verbose);
6170
  }
6171
 
6172
- /* IQ1_S via the HPC pipeline (same machinery as Q2_K, IQ1_S geometry).
6173
- * n_elements must be a multiple of 256; output needs n/256*50 bytes. */
6174
- void hexstate_quantize_tensor_iq1_s(const float *weights, int64_t n_elements,
6175
- void *output, float *out_error,
6176
- int verbose)
6177
- {
6178
- hexstate_init();
6179
- if (!weights || !output || n_elements <= 0 || n_elements % QK_K != 0) {
6180
- if (out_error) *out_error = -1.0f;
6181
- return;
6182
- }
6183
- {
6184
- /* uniform importance: weight = sqrt(sigma2 + x^2) */
6185
- float *wbuf = (float *)malloc((size_t)n_elements * sizeof(float));
6186
- int64_t i;
6187
- if (!wbuf) { if (out_error) *out_error = -1.0f; return; }
6188
- for (i = 0; i < n_elements; ++i) wbuf[i] = 1.0f;
6189
- quantize_tensor_iq1_s_hpc(weights, n_elements,
6190
- (BlockIQ1S *)output, out_error,
6191
- wbuf, verbose);
6192
- free(wbuf);
6193
- }
6194
- }
6195
-
6196
- void hexstate_quantize_tensor_iq1_s_imat(const float *weights, int64_t n_elements,
6197
- void *output, float *out_error,
6198
- const float *imat_importance,
6199
- int verbose)
6200
- {
6201
- hexstate_init();
6202
- quantize_tensor_iq1_s_hpc(weights, n_elements,
6203
- (BlockIQ1S *)output, out_error,
6204
- imat_importance, verbose);
6205
- }
6206
-
6207
- int hexstate_iq1s_block_bytes(void) { return (int)sizeof(BlockIQ1S); }
6208
- int hexstate_iq1s_block_elements(void) { return QK_K; }
6209
-
6210
- /* Debug/test: dequantize one block. */
6211
- void hexstate_iq1s_dequant_dbg(const void *block, float *out) {
6212
- hex_iq1s_init();
6213
- hex_iq1s_dequant_block((const BlockIQ1S *)block, out);
6214
- }
6215
-
6216
  #ifndef HEXSTATE_LIBRARY
6217
  /* ═══════════════════════════════════════════════════════════════════════════
6218
  * MAIN
 
5239
  * ═══════════════════════════════════════════════════════════════════════════ */
5240
 
5241
  /* Initialize HExState subsystems (must be called once before quantization) */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5242
  void hexstate_init(void)
5243
  {
5244
  static int initialized = 0;
 
5322
  imat_importance, verbose);
5323
  }
5324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5325
  #ifndef HEXSTATE_LIBRARY
5326
  /* ═══════════════════════════════════════════════════════════════════════════
5327
  * MAIN
hexstate_requantize.py CHANGED
@@ -96,24 +96,6 @@ def _load_hexstate_lib():
96
  ctypes.c_int, # verbose
97
  ]
98
 
99
- # IQ1_S HPC quantizer (same HPC machinery as Q2_K, IQ1_S geometry)
100
- if hasattr(lib, 'hexstate_quantize_tensor_iq1_s_imat'):
101
- lib.hexstate_quantize_tensor_iq1_s_imat.restype = None
102
- lib.hexstate_quantize_tensor_iq1_s_imat.argtypes = [
103
- ctypes.POINTER(ctypes.c_float), # weights
104
- ctypes.c_int64, # n_elements
105
- ctypes.c_void_p, # output
106
- ctypes.POINTER(ctypes.c_float), # out_error
107
- ctypes.POINTER(ctypes.c_float), # imat_importance (can be NULL)
108
- ctypes.c_int, # verbose
109
- ]
110
-
111
- if hasattr(lib, 'hexstate_iq1s_block_bytes'):
112
- lib.hexstate_iq1s_block_bytes.restype = ctypes.c_int
113
- lib.hexstate_iq1s_block_bytes.argtypes = []
114
- lib.hexstate_iq1s_block_elements.restype = ctypes.c_int
115
- lib.hexstate_iq1s_block_elements.argtypes = []
116
-
117
  lib.hexstate_init()
118
  _HEXSTATE_LIB = lib
119
  return lib
@@ -278,56 +260,6 @@ def quantize_tensor_q2k_hpc(f32_data, opt_mode=2, importance=None):
278
  return output.tobytes(), n_blocks
279
 
280
 
281
- def quantize_tensor_iq1_s_hpc(f32_data, importance=None):
282
- """Quantize tensor to IQ1_S using the HexState HPC C implementation.
283
-
284
- Verbatim counterpart of quantize_tensor_q2k_hpc: same padding,
285
- same buffer handling, same imatrix plumbing; only the C entry
286
- point and block geometry (50 B / 256 el) differ.
287
- importance: optional per-element importance weights (from imatrix)
288
- Returns: (bytes, n_blocks, rmse)
289
- """
290
- lib = _load_hexstate_lib()
291
- if lib is None:
292
- raise RuntimeError("HexState library not available")
293
-
294
- n_elements = len(f32_data)
295
- if n_elements % QK_K != 0:
296
- pad_len = QK_K - (n_elements % QK_K)
297
- f32_data = np.concatenate([f32_data, np.zeros(pad_len, dtype=np.float32)])
298
- if importance is not None:
299
- importance = np.concatenate([importance, np.ones(pad_len, dtype=np.float32)])
300
- n_elements = len(f32_data)
301
-
302
- n_blocks = n_elements // QK_K
303
- block_bytes = lib.hexstate_iq1s_block_bytes() # 50
304
-
305
- # Allocate output buffer
306
- output = np.zeros(n_blocks * block_bytes, dtype=np.uint8)
307
- error = ctypes.c_float(0.0)
308
-
309
- # Call C quantizer with or without importance weights
310
- f32_contiguous = np.ascontiguousarray(f32_data, dtype=np.float32)
311
-
312
- if importance is not None:
313
- imat_contiguous = np.ascontiguousarray(importance, dtype=np.float32)
314
- imat_ptr = imat_contiguous.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
315
- else:
316
- imat_ptr = None
317
-
318
- lib.hexstate_quantize_tensor_iq1_s_imat(
319
- f32_contiguous.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
320
- ctypes.c_int64(n_elements),
321
- output.ctypes.data_as(ctypes.c_void_p),
322
- ctypes.byref(error),
323
- imat_ptr,
324
- ctypes.c_int(1), # verbose
325
- )
326
-
327
- rmse = float(np.sqrt(max(error.value, 0.0) / max(n_elements, 1)))
328
- return output.tobytes(), n_blocks, rmse
329
-
330
-
331
  # ─── Constants ──────────────────────────────────────────────────────────────
332
  GGUF_MAGIC = 0x46554747
333
  GGUF_VERSION = 3
@@ -339,27 +271,24 @@ GGML_TYPE_F16 = 1
339
  GGML_TYPE_Q4_0 = 2
340
  GGML_TYPE_Q8_0 = 8
341
  GGML_TYPE_Q2_K = 10
342
- GGML_TYPE_IQ1_S = 19
343
  GGML_TYPE_BF16 = 30
344
 
345
- IQ1S_TYPE_SIZE = 50 # bytes per 256-element IQ1_S block
346
-
347
  TYPE_NAME = {
348
  0: "F32", 1: "F16", 2: "Q4_0", 3: "Q4_1", 6: "Q5_0", 7: "Q5_1",
349
  8: "Q8_0", 9: "Q8_1", 10: "Q2_K", 11: "Q3_K", 12: "Q4_K",
350
- 13: "Q5_K", 14: "Q6_K", 15: "Q8_K", 19: "IQ1_S", 30: "BF16",
351
  }
352
 
353
  # Block sizes and byte sizes for each type
354
  TYPE_BLOCK_SIZE = {
355
  0: 1, 1: 1, 2: 32, 3: 32, 6: 32, 7: 32,
356
  8: 32, 9: 32, 10: 256, 11: 256, 12: 256,
357
- 13: 256, 14: 256, 15: 256, 19: 256, 30: 1,
358
  }
359
  TYPE_BLOCK_BYTES = {
360
  0: 4, 1: 2, 2: 18, 3: 20, 6: 20, 7: 22,
361
  8: 34, 9: 36, 10: 84, 11: 110, 12: 144,
362
- 13: 176, 14: 210, 15: 292, 19: 50, 30: 2,
363
  }
364
 
365
 
@@ -723,9 +652,6 @@ def is_q4_tensor(name):
723
  Attention/FFN are NOT matched here β€” they keep their Q2_K plan.
724
  NOTE: 'output.weight' is exact-matched so 'attn_output.weight'
725
  never matches.
726
- NOTE 2: ssm_beta is deliberately NOT matched here β€” the default Q2_K
727
- pipeline ships it at Q2_K (proven good). Only the --iq1-all branch
728
- promotes it (see main()), because 1-bit recurrent dynamics diverge.
729
  """
730
  if name == 'output.weight' or name.endswith('.output.weight'):
731
  return True
@@ -838,7 +764,7 @@ def should_quantize(name, n_dims, dims, tied_embeddings=False):
838
  def main():
839
  if len(sys.argv) < 3:
840
  print("Usage: python3 hexstate_requantize.py <input.gguf> <output.gguf>"
841
- " [--keep-metadata] [--imatrix FILE] [--keep-embd] [--q2all] [--q2kall] [--iq1-all]")
842
  sys.exit(1)
843
 
844
  input_path = sys.argv[1]
@@ -846,8 +772,6 @@ def main():
846
  keep_metadata = '--keep-metadata' in sys.argv
847
  quantize_none = '--quantize-none' in sys.argv
848
  q2all = '--q2all' in sys.argv
849
- q2kall = '--q2kall' in sys.argv # verbatim duplicate of --q2all
850
- iq1all = '--iq1-all' in sys.argv # verbatim duplicate of --q2all, IQ1_S target
851
  keep_embd = '--keep-embd' in sys.argv # keep tied embedding at source precision instead of Q8_0
852
 
853
  # Check for imatrix
@@ -869,10 +793,8 @@ def main():
869
  print(" ╔════════════════════════════════════════════════════════════════╗")
870
  print(" β•‘ HExState GGUF Re-Quantizer β•‘")
871
  print(" β•‘ GGUF β†’ Q2_K GGUF with metadata passthrough β•‘")
872
- if q2all or q2kall:
873
- print(" β•‘ Mode: --q2all/--q2kall ALL eligible tensors β†’ Q2_K (test mode) β•‘")
874
- if iq1all:
875
- print(" β•‘ Mode: --iq1-all ALL eligible tensors β†’ IQ1_S (test mode) β•‘")
876
  if use_hpc and imatrix_data:
877
  print(" β•‘ Engine: HPC + iMatrix (calibrated sensitivity propagation) β•‘")
878
  elif use_hpc:
@@ -959,21 +881,6 @@ def main():
959
  total_attn = 0
960
  total_keep = 0
961
  total_embd = 0
962
- # For --iq1-all: replicate llama.cpp's MOSTLY_IQ1_S preset mix with
963
- # the types this pipeline supports (Q2_K/Q4_0-HPC/IQ1_S):
964
- # attn_v (separate) -> Q4 (preset: Q4_K; V carries content)
965
- # attn/ssm output -> Q4 (preset: IQ2_XXS for attn_o)
966
- # token_embd -> Q2_K (preset: Q2_K)
967
- # ffn_down first 1/8 layers -> Q2_K (preset: Q2_K)
968
- # rest (attn_q/k incl. fused qkv, ffn_up/gate, ffn_down) -> IQ1_S
969
- # Pure-1-bit attention (esp. V) severs contextual linkage while
970
- # leaving local fluency intact β€” the observed failure mode.
971
- def _ti_bid(name):
972
- for part in name.split('.'):
973
- if part.isdecimal():
974
- return int(part)
975
- return None
976
- n_ffn_down = sum(1 for ti in tensor_infos if '.ffn_down.weight' in ti['name'])
977
  for ti in tensor_infos:
978
  if quantize_none:
979
  will_quant = False
@@ -989,7 +896,7 @@ def main():
989
  will_quant = 'EMBD_Q8'
990
  total_embd += 1
991
  elif should_quantize(ti['name'], ti['n_dims'], ti['dims'], tied_embeddings):
992
- if q2all or q2kall:
993
  if q2k_row_compatible(ti['n_dims'], ti['dims']):
994
  will_quant = True; total_quant += 1
995
  elif q4_row_compatible(ti['n_dims'], ti['dims']):
@@ -998,41 +905,6 @@ def main():
998
  else:
999
  will_quant = False; total_keep += 1
1000
  print(f" Q2ALLβ†’KEEP (row width not block-aligned): {ti['name']}")
1001
- elif iq1all:
1002
- nm = ti['name']
1003
- is_sep_v = ('.attn_v.weight' in nm or 'v_proj.weight' in nm) \
1004
- and 'qkv' not in nm
1005
- is_out = ('.attn_output.weight' in nm or 'o_proj.weight' in nm
1006
- or 'ssm_out.weight' in nm)
1007
- is_embd = (nm == 'token_embd.weight' or '.token_embd.weight' in nm)
1008
- bid = _ti_bid(nm)
1009
- is_early_ffn = ('.ffn_down.weight' in nm and bid is not None
1010
- and n_ffn_down > 0 and bid < (n_ffn_down + 7) // 8)
1011
- if ((is_q4_tensor(nm) and not is_embd) or 'ssm_beta.weight' in nm) \
1012
- and q4_row_compatible(ti['n_dims'], ti['dims']):
1013
- # Q4-class tensors (output/ssm_alpha) stay Q4_0-HPC.
1014
- # ssm_beta joins them ONLY here: it is the recurrent
1015
- # partner of ssm_alpha and 1-bit dynamics diverge.
1016
- # Default mode still ships ssm_beta at Q2_K (proven).
1017
- will_quant = 'Q4_HPC'; total_attn += 1
1018
- elif (is_sep_v or is_out) \
1019
- and q4_row_compatible(ti['n_dims'], ti['dims']):
1020
- # Preset keeps V at Q4_K and O at IQ2_XXS; closest
1021
- # available is Q4_0-HPC. (Fused qkv can't split and
1022
- # stays IQ1_S like the preset's Q/K.)
1023
- will_quant = 'Q4_HPC'; total_attn += 1
1024
- elif (is_embd or is_early_ffn) \
1025
- and q2k_row_compatible(ti['n_dims'], ti['dims']):
1026
- # Preset: embeddings and first-1/8 FFN-down at Q2_K.
1027
- will_quant = True; total_quant += 1
1028
- elif q2k_row_compatible(ti['n_dims'], ti['dims']):
1029
- will_quant = 'IQ1_S'; total_quant += 1
1030
- elif q4_row_compatible(ti['n_dims'], ti['dims']):
1031
- will_quant = 'Q4_0'; total_quant += 1
1032
- print(f" IQ1ALL→Q4_0 (row width not 256-aligned): {ti['name']}")
1033
- else:
1034
- will_quant = False; total_keep += 1
1035
- print(f" IQ1ALL→KEEP (row width not block-aligned): {ti['name']}")
1036
  elif is_q4_tensor(ti['name']) and q4_row_compatible(ti['n_dims'], ti['dims']):
1037
  # Q4_0 path (HPC sieve). dim0 β€” not just total n β€” must be
1038
  # block-aligned: GGUF quants tile along rows and llama.cpp
@@ -1049,15 +921,10 @@ def main():
1049
  total_keep += 1
1050
  quant_plan.append(will_quant)
1051
 
1052
- if q2all or q2kall:
1053
- print(" Mode: --q2all/--q2kall β€” Q2_K wherever block-compatible; safe Q4/keep otherwise")
1054
  print(f" Tensors to quantize (Q2_K): {total_quant}")
1055
  print(f" Tensors to keep as-is: {total_keep}")
1056
- elif iq1all:
1057
- print(" Mode: --iq1-all β€” IQ1_S wherever block-compatible; safe Q4/keep otherwise")
1058
- print(f" Tensors to quantize (IQ1_S): {total_quant}")
1059
- print(f" Tensors to promote (Q4_0Β·HPC): {total_attn}")
1060
- print(f" Tensors to keep as-is: {total_keep}")
1061
  else:
1062
  print(f" Tensors to quantize (Q2_K): {total_quant}")
1063
  print(f" Tensors to promote (Q4_0Β·HPC): {total_attn}")
@@ -1091,11 +958,6 @@ def main():
1091
  n_blocks = ti['n_elements'] // 32
1092
  out_size = n_blocks * 18
1093
  print(f" Q4_0: {ti['name']} (dims[0]={dim0})")
1094
- elif quant_plan[i] == 'IQ1_S':
1095
- out_type = GGML_TYPE_IQ1_S
1096
- n_blocks = ti['n_elements'] // QK_K
1097
- out_size = n_blocks * IQ1S_TYPE_SIZE
1098
- print(f" [IQ1_SΒ·HPC] {ti['name']} ({ti['n_elements']:,} elements)")
1099
  elif quant_plan[i] is True and q2k_row_compatible(ti['n_dims'], ti['dims']):
1100
  out_type = GGML_TYPE_Q2_K
1101
  n_blocks = ti['n_elements'] // QK_K
@@ -1129,8 +991,8 @@ def main():
1129
  else:
1130
  for key, vtype, raw_value in kv_pairs:
1131
  if key == 'general.file_type' and vtype == 4: # UINT32
1132
- # file_type=10 means Q2_K, 24 means IQ1_S in llama.cpp
1133
- updated_kv.append((key, vtype, struct.pack('<I', 24 if iq1all else 10)))
1134
  elif key == 'general.quantization_version' and vtype == 4:
1135
  updated_kv.append((key, vtype, struct.pack('<I', 2)))
1136
  elif key == 'tokenizer.ggml.token_type' and vtype == 9:
@@ -1378,74 +1240,6 @@ def main():
1378
  quant_count += 1
1379
  total_quant_bytes += n_blocks_q4 * 18
1380
 
1381
- elif quant_plan[i] == 'IQ1_S':
1382
- # Convert to F32 for quantization
1383
- if ti['type'] == GGML_TYPE_BF16:
1384
- f32 = bf16_to_f32(raw_data, ti['n_elements'])
1385
- elif ti['type'] == GGML_TYPE_F16:
1386
- f32 = f16_to_f32(raw_data, ti['n_elements'])
1387
- elif ti['type'] == GGML_TYPE_F32:
1388
- f32 = np.frombuffer(raw_data, dtype=np.float32).copy()
1389
- else:
1390
- # Can't re-quantize from quantized format β€” keep as-is
1391
- fout.write(raw_data)
1392
- pad = align_offset(fout.tell()) - fout.tell()
1393
- if pad > 0:
1394
- fout.write(b'\x00' * pad)
1395
- continue
1396
-
1397
- # Quantize to IQ1_S β€” always use HPC with chunked processing
1398
- # Each chunk gets full HPC treatment (no size threshold)
1399
- row_width = int(ti['dims'][0])
1400
- HPC_CHUNK = max(row_width, (50_000_000 // row_width) * row_width)
1401
-
1402
- # Look up imatrix importance for this tensor
1403
- imat_full = _expand_imatrix(ti, imatrix_data)
1404
-
1405
- n_el = ti['n_elements']
1406
- if use_hpc and n_el <= HPC_CHUNK:
1407
- # Small tensor β€” single HPC pass
1408
- iq1_data, n_blocks, tensor_rmse = quantize_tensor_iq1_s_hpc(f32, importance=imat_full)
1409
- elif use_hpc:
1410
- # Large tensor β€” chunked HPC (each chunk gets BP)
1411
- chunks = []
1412
- processed = 0
1413
- se_sum = 0.0
1414
- while processed < n_el:
1415
- end = min(processed + HPC_CHUNK, n_el)
1416
- chunk_f32 = f32[processed:end]
1417
- if len(chunk_f32) % QK_K != 0:
1418
- pad_len = QK_K - (len(chunk_f32) % QK_K)
1419
- chunk_f32 = np.concatenate([chunk_f32, np.zeros(pad_len, dtype=np.float32)])
1420
- chunk_imp = imat_full[processed:end] if imat_full is not None else None
1421
- if chunk_imp is not None and len(chunk_imp) < len(chunk_f32):
1422
- chunk_imp = np.concatenate([chunk_imp, np.ones(len(chunk_f32) - len(chunk_imp), dtype=np.float32)])
1423
- chunk_data, _, chunk_rmse = quantize_tensor_iq1_s_hpc(chunk_f32, importance=chunk_imp)
1424
- actual_blocks = (end - processed + QK_K - 1) // QK_K
1425
- chunks.append(chunk_data[:actual_blocks * IQ1S_TYPE_SIZE])
1426
- se_sum += (chunk_rmse ** 2) * (end - processed)
1427
- processed = end
1428
- pct = 100.0 * processed / n_el
1429
- print(f"\r β†’ {processed/1e6:.0f}M/{n_el/1e6:.0f}M ({pct:.0f}%)", end='', flush=True)
1430
- print()
1431
- iq1_data = b''.join(chunks)
1432
- n_blocks = n_el // QK_K
1433
- tensor_rmse = float(np.sqrt(se_sum / max(n_el, 1)))
1434
- else:
1435
- # No HPC available β€” IQ1_S needs the C library; keep as-is
1436
- fout.write(raw_data)
1437
- pad = align_offset(fout.tell()) - fout.tell()
1438
- if pad > 0:
1439
- fout.write(b'\x00' * pad)
1440
- print(f" [IQ1_S] {ti['name'][:55]} skipped (no HPC library)")
1441
- continue
1442
- fout.write(iq1_data)
1443
- print(f" [IQ1_SΒ·HPC] {ti['name'][:55]} RMSE={tensor_rmse:.6e}")
1444
- q2k_rmse_sum += tensor_rmse
1445
- q2k_tensor_count += 1
1446
-
1447
- quant_count += 1
1448
- total_quant_bytes += len(iq1_data)
1449
  elif quant_plan[i]:
1450
  # Convert to F32 for quantization
1451
  if ti['type'] == GGML_TYPE_BF16:
@@ -1559,16 +1353,16 @@ def main():
1559
  print(" ╔════════════════════════════════════════════════════════════════╗")
1560
  print(" β•‘ RE-QUANTIZATION SUMMARY β•‘")
1561
  print(" ╠════════════════════════════════════════════════════════════════╣")
1562
- print(f" β•‘ Tensors quantized ({'IQ1_S' if iq1all else 'Q2_K'}): {quant_count:<33d} β•‘")
1563
  print(f" β•‘ Tensors kept as-is: {total_keep:<33d} β•‘")
1564
- print(f" β•‘ {'IQ1_S' if iq1all else 'Q2_K'} data: {total_quant_bytes:>12,} bytes ({total_quant_bytes/1024**2:>7.1f} MB) β•‘")
1565
  print(f" β•‘ Kept data: {total_keep_bytes:>12,} bytes ({total_keep_bytes/1024**2:>7.1f} MB) β•‘")
1566
  print(f" β•‘ Original size: {file_size:>12,} bytes ({file_size/1024**3:>7.2f} GB) β•‘")
1567
  print(f" β•‘ Output size: {final_size:>12,} bytes ({final_size/1024**3:>7.2f} GB) β•‘")
1568
  print(f" β•‘ Compression: {compression:>42.1f}x β•‘")
1569
  if q2k_tensor_count > 0:
1570
  mean_rmse = q2k_rmse_sum / q2k_tensor_count
1571
- print(f" β•‘ Mean {'IQ1_S' if iq1all else 'Q2_K'} RMSE: {mean_rmse:>12.6e} β•‘")
1572
  print(f" β•‘ Total time: {elapsed:>39.1f} sec β•‘")
1573
  print(" β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•")
1574
  print()
 
96
  ctypes.c_int, # verbose
97
  ]
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  lib.hexstate_init()
100
  _HEXSTATE_LIB = lib
101
  return lib
 
260
  return output.tobytes(), n_blocks
261
 
262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
  # ─── Constants ──────────────────────────────────────────────────────────────
264
  GGUF_MAGIC = 0x46554747
265
  GGUF_VERSION = 3
 
271
  GGML_TYPE_Q4_0 = 2
272
  GGML_TYPE_Q8_0 = 8
273
  GGML_TYPE_Q2_K = 10
 
274
  GGML_TYPE_BF16 = 30
275
 
 
 
276
  TYPE_NAME = {
277
  0: "F32", 1: "F16", 2: "Q4_0", 3: "Q4_1", 6: "Q5_0", 7: "Q5_1",
278
  8: "Q8_0", 9: "Q8_1", 10: "Q2_K", 11: "Q3_K", 12: "Q4_K",
279
+ 13: "Q5_K", 14: "Q6_K", 15: "Q8_K", 30: "BF16",
280
  }
281
 
282
  # Block sizes and byte sizes for each type
283
  TYPE_BLOCK_SIZE = {
284
  0: 1, 1: 1, 2: 32, 3: 32, 6: 32, 7: 32,
285
  8: 32, 9: 32, 10: 256, 11: 256, 12: 256,
286
+ 13: 256, 14: 256, 15: 256, 30: 1,
287
  }
288
  TYPE_BLOCK_BYTES = {
289
  0: 4, 1: 2, 2: 18, 3: 20, 6: 20, 7: 22,
290
  8: 34, 9: 36, 10: 84, 11: 110, 12: 144,
291
+ 13: 176, 14: 210, 15: 292, 30: 2,
292
  }
293
 
294
 
 
652
  Attention/FFN are NOT matched here β€” they keep their Q2_K plan.
653
  NOTE: 'output.weight' is exact-matched so 'attn_output.weight'
654
  never matches.
 
 
 
655
  """
656
  if name == 'output.weight' or name.endswith('.output.weight'):
657
  return True
 
764
  def main():
765
  if len(sys.argv) < 3:
766
  print("Usage: python3 hexstate_requantize.py <input.gguf> <output.gguf>"
767
+ " [--keep-metadata] [--imatrix FILE] [--keep-embd] [--q2all]")
768
  sys.exit(1)
769
 
770
  input_path = sys.argv[1]
 
772
  keep_metadata = '--keep-metadata' in sys.argv
773
  quantize_none = '--quantize-none' in sys.argv
774
  q2all = '--q2all' in sys.argv
 
 
775
  keep_embd = '--keep-embd' in sys.argv # keep tied embedding at source precision instead of Q8_0
776
 
777
  # Check for imatrix
 
793
  print(" ╔════════════════════════════════════════════════════════════════╗")
794
  print(" β•‘ HExState GGUF Re-Quantizer β•‘")
795
  print(" β•‘ GGUF β†’ Q2_K GGUF with metadata passthrough β•‘")
796
+ if q2all:
797
+ print(" β•‘ Mode: --q2all ALL eligible tensors β†’ Q2_K (test mode) β•‘")
 
 
798
  if use_hpc and imatrix_data:
799
  print(" β•‘ Engine: HPC + iMatrix (calibrated sensitivity propagation) β•‘")
800
  elif use_hpc:
 
881
  total_attn = 0
882
  total_keep = 0
883
  total_embd = 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
884
  for ti in tensor_infos:
885
  if quantize_none:
886
  will_quant = False
 
896
  will_quant = 'EMBD_Q8'
897
  total_embd += 1
898
  elif should_quantize(ti['name'], ti['n_dims'], ti['dims'], tied_embeddings):
899
+ if q2all:
900
  if q2k_row_compatible(ti['n_dims'], ti['dims']):
901
  will_quant = True; total_quant += 1
902
  elif q4_row_compatible(ti['n_dims'], ti['dims']):
 
905
  else:
906
  will_quant = False; total_keep += 1
907
  print(f" Q2ALLβ†’KEEP (row width not block-aligned): {ti['name']}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
908
  elif is_q4_tensor(ti['name']) and q4_row_compatible(ti['n_dims'], ti['dims']):
909
  # Q4_0 path (HPC sieve). dim0 β€” not just total n β€” must be
910
  # block-aligned: GGUF quants tile along rows and llama.cpp
 
921
  total_keep += 1
922
  quant_plan.append(will_quant)
923
 
924
+ if q2all:
925
+ print(" Mode: --q2all β€” Q2_K wherever block-compatible; safe Q4/keep otherwise")
926
  print(f" Tensors to quantize (Q2_K): {total_quant}")
927
  print(f" Tensors to keep as-is: {total_keep}")
 
 
 
 
 
928
  else:
929
  print(f" Tensors to quantize (Q2_K): {total_quant}")
930
  print(f" Tensors to promote (Q4_0Β·HPC): {total_attn}")
 
958
  n_blocks = ti['n_elements'] // 32
959
  out_size = n_blocks * 18
960
  print(f" Q4_0: {ti['name']} (dims[0]={dim0})")
 
 
 
 
 
961
  elif quant_plan[i] is True and q2k_row_compatible(ti['n_dims'], ti['dims']):
962
  out_type = GGML_TYPE_Q2_K
963
  n_blocks = ti['n_elements'] // QK_K
 
991
  else:
992
  for key, vtype, raw_value in kv_pairs:
993
  if key == 'general.file_type' and vtype == 4: # UINT32
994
+ # file_type=10 means Q2_K in llama.cpp
995
+ updated_kv.append((key, vtype, struct.pack('<I', 10)))
996
  elif key == 'general.quantization_version' and vtype == 4:
997
  updated_kv.append((key, vtype, struct.pack('<I', 2)))
998
  elif key == 'tokenizer.ggml.token_type' and vtype == 9:
 
1240
  quant_count += 1
1241
  total_quant_bytes += n_blocks_q4 * 18
1242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1243
  elif quant_plan[i]:
1244
  # Convert to F32 for quantization
1245
  if ti['type'] == GGML_TYPE_BF16:
 
1353
  print(" ╔════════════════════════════════════════════════════════════════╗")
1354
  print(" β•‘ RE-QUANTIZATION SUMMARY β•‘")
1355
  print(" ╠════════════════════════════════════════════════════════════════╣")
1356
+ print(f" β•‘ Tensors quantized (Q2_K): {quant_count:<33d} β•‘")
1357
  print(f" β•‘ Tensors kept as-is: {total_keep:<33d} β•‘")
1358
+ print(f" β•‘ Q2_K data: {total_quant_bytes:>12,} bytes ({total_quant_bytes/1024**2:>7.1f} MB) β•‘")
1359
  print(f" β•‘ Kept data: {total_keep_bytes:>12,} bytes ({total_keep_bytes/1024**2:>7.1f} MB) β•‘")
1360
  print(f" β•‘ Original size: {file_size:>12,} bytes ({file_size/1024**3:>7.2f} GB) β•‘")
1361
  print(f" β•‘ Output size: {final_size:>12,} bytes ({final_size/1024**3:>7.2f} GB) β•‘")
1362
  print(f" β•‘ Compression: {compression:>42.1f}x β•‘")
1363
  if q2k_tensor_count > 0:
1364
  mean_rmse = q2k_rmse_sum / q2k_tensor_count
1365
+ print(f" β•‘ Mean Q2_K RMSE: {mean_rmse:>12.6e} β•‘")
1366
  print(f" β•‘ Total time: {elapsed:>39.1f} sec β•‘")
1367
  print(" β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•")
1368
  print()