CompressedGemma commited on
Commit
ae547e6
Β·
verified Β·
1 Parent(s): a034f4d

Replace Shor's with accelerated sieve

Browse files
Files changed (2) hide show
  1. hexstate_quantize.c +237 -55
  2. hexstate_requantize.py +8 -8
hexstate_quantize.c CHANGED
@@ -19,7 +19,9 @@
19
  * ─────────────────────────────────────────────────
20
  * HPCGraph + CZ edges β†’ Block sensitivity graph
21
  * Complex Amplitude BP β†’ Importance propagation
22
- * MCMC period sampler β†’ Optimal scale search
 
 
23
  * try_period() validation β†’ Error bound checking
24
  * LLL lattice reduction β†’ (future) Adaptive bit allocation
25
  *
@@ -648,7 +650,7 @@ static int is_attention_tensor(const char *gguf_name)
648
  *
649
  * Nothing in the live pipeline calls the legacy BP sensitivity graph
650
  * (build_sensitivity_graph + compute_block_error_q2k + SCALE_TABLE) or the
651
- * llm-compressor MSE grid search (mse_grid_search_q2k_subblock); the Shor /
652
  * Viterbi path superseded them. They are preserved behind this flag instead
653
  * of silently shipping as dead code that still costs an init pass.
654
  * ════════════════════════════════════════════════════════════════════════ */
@@ -675,7 +677,7 @@ static void init_scale_table(void) {
675
  /* ═══════════════════════════════════════════════════════════════════════════
676
  * THREAD-LOCAL HPCGRAPH REUSE β€” Eliminates 776K malloc/free cycles
677
  *
678
- * The sub-block Shor measurement uses a 16-node linear-chain graph that
679
  * is identical in topology every time. Instead of hpc_create()/hpc_destroy()
680
  * inside the OMP hot loop, we reset the same graph to a clean state.
681
  *
@@ -1209,14 +1211,10 @@ static float hpc_make_qp_quants(int n, int nmax, const float *x,
1209
  * (HPC_W6_RE / HPC_W6_IM) β€” the file-local duplicates were unused. */
1210
  static const double INV_SQRT6 = 0.40824829046386301637; /* 1/√6 */
1211
 
1212
- /* ── Collapse + Back-Action core (ported from tesseract_factor.c) ──
1213
- * After sampling an outcome, collapse the target site to |outcome⟩,
1214
- * absorb all edge weights into neighbor local states (Magic Pointer
1215
- * disentanglement), and remove dead edges from the graph.
1216
- *
1217
- * This is the EXACT same back-action protocol used in Shor's algorithm
1218
- * for the semi-classical QFT: measurement of one site conditions all
1219
- * remaining sites through the CZ phase correlations. */
1220
  static void shor_collapse_site(HPCGraph *graph, int target_site, int outcome)
1221
  {
1222
  /* Step 1: Collapse local state to |outcome⟩ */
@@ -1286,22 +1284,8 @@ static void shor_collapse_site(HPCGraph *graph, int target_site, int outcome)
1286
  }
1287
 
1288
  /* ═══════════════════════════════════════════════════════════════════════════
1289
- * SHOR SEQUENTIAL MEASUREMENT β€” Griffiths-Niu Protocol for Quantization
1290
- *
1291
- * Ported 1:1 from tesseract_factor.c lines 2343-2500.
1292
- *
1293
- * Measures sites MSB→LSB. For each site k:
1294
- * 1. Compute feed-forward phase correction ΞΈ_k from previously measured sites
1295
- * 2. Compute neighbor contribution C_k(d) analytically
1296
- * 3. Bake C_k into locals
1297
- * 4. Apply phase correction: Ξ±(d) *= e^{-2Ο€i d ΞΈ_k}
1298
- * 5. Apply IDFT6: Ξ²(v) = (1/√6) Ξ£_d Ξ±'(d) Γ— e^{2Ο€i dv/6}
1299
- * 6. Compute |Ξ²(v)|Β² as measurement probabilities
1300
- * 7. Sample/argmax β†’ outcome
1301
- * 8. Collapse + back-action via shor_collapse_site()
1302
- *
1303
- * Returns: marginals are written into marg_out[n_sites][6].
1304
- * measured_out[n_sites] receives the measurement outcomes.
1305
  * ═══════════════════════════════════════════════════════════════════════════ */
1306
  static void shor_measure_graph(HPCGraph *graph, int64_t n_sites,
1307
  double (*marg_out)[6], int *measured_out,
@@ -1453,6 +1437,204 @@ static void shor_measure_graph(HPCGraph *graph, int64_t n_sites,
1453
  }
1454
  }
1455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1456
  /* ═══════════════════════════════════════════════════════════════════════════
1457
  * HPC-OPTIMIZED Q4_0 QUANTIZATION (for attention tensors)
1458
  *
@@ -1766,14 +1948,14 @@ static void quantize_tensor_q4_0_hpc(const float *weights, int64_t n_elements,
1766
  for (int64_t i = 0; i < graph_blocks - 1; i++)
1767
  hpc_cz(graph, i, i + 1);
1768
 
1769
- /* ── Shor's Griffiths-Niu Sequential Measurement ──
1770
- * Replaces BP with exact marginals via IDFT6 + feed-forward +
1771
- * collapse/back-action (ported 1:1 from tesseract_factor.c).
1772
  * Single pass, no iteration, no message damping. */
1773
  double (*marg)[6] = (double (*)[6])calloc(graph_blocks, sizeof(double[6]));
1774
  int *shor_measured = (int *)calloc(graph_blocks, sizeof(int));
1775
 
1776
- shor_measure_graph(graph, graph_blocks, marg, shor_measured, 1);
1777
 
1778
  free(shor_measured);
1779
 
@@ -2248,12 +2430,12 @@ static void quantize_tensor_q4_0_hpc(const float *weights, int64_t n_elements,
2248
  }
2249
 
2250
  /* ════════════════════════════════════════════════════════════════════════
2251
- * Q8_0 HPC QUANTIZER β€” Shor pipeline at 8 bits
2252
  *
2253
  * Same pipeline as Q4_0: WLS scale + tight candidate grid scored on the
2254
  * extended objective (weighted SSE + DC + vesica/wave), triality-quhit
2255
  * graph with Boltzmann-encoded candidate errors, CZ chain entanglement,
2256
- * Shor Griffiths-Niu sequential measurement for bin consensus, greedy
2257
  * override (HEX_GREEDY_OVERRIDE_RATIO), then per-block ULP polish, the
2258
  * vesica/DC error-shaping descent with an extended-objective guard, and
2259
  * the candidate floor. Intended for embedding / LM-head tensors (tied
@@ -2363,7 +2545,7 @@ static void quantize_tensor_q8_0_hpc(const float *weights, int64_t n_elements,
2363
  best_candidate[blk] = 11; /* Γ—1.0000 neutral seed */
2364
  }
2365
 
2366
- /* ── Phase 3: Shor graph β€” triality quhits, CZ chain, GN measurement ── */
2367
  int shor_ran = 0;
2368
  if (n_blocks >= 2) {
2369
  int64_t graph_blocks = (n_blocks > 200) ? 200 : n_blocks;
@@ -2435,9 +2617,9 @@ static void quantize_tensor_q8_0_hpc(const float *weights, int64_t n_elements,
2435
  double (*marg)[6] = (double (*)[6])calloc(graph_blocks, sizeof(double[6]));
2436
  int *measured = (int *)calloc(graph_blocks, sizeof(int));
2437
  if (marg && measured) {
2438
- shor_measure_graph(graph, graph_blocks, marg, measured, 1);
2439
 
2440
- /* Per-block selection: best candidate inside the Shor-
2441
  * measured bin, then greedy override against the global
2442
  * argmin β€” identical Step-F semantics to Q2_K/Q4_0. */
2443
  for (int64_t i = 0; i < graph_blocks; i++) {
@@ -2880,7 +3062,7 @@ static void quantize_tensor_q2k_hpc(const float *weights, int64_t n_elements,
2880
  }
2881
 
2882
  /* ══════════════════════════════════════════════════════════════════
2883
- * PHASE 3: HPC Graph β€” Shor's Griffiths-Niu Measurement
2884
  * ══════════════════════════════════════════════════════════════════ */
2885
 
2886
  int *best_candidate = (int *)malloc(n_blocks * sizeof(int));
@@ -3003,7 +3185,7 @@ static void quantize_tensor_q2k_hpc(const float *weights, int64_t n_elements,
3003
  double (*shor_marg)[6] = (double (*)[6])calloc(n_sites, sizeof(double[6]));
3004
  int *shor_measured = (int *)calloc(n_sites, sizeof(int));
3005
 
3006
- shor_measure_graph(graph, n_sites, shor_marg, shor_measured, 1);
3007
 
3008
  double (*coarse_marg)[6] = (double (*)[6])calloc(graph_blocks, sizeof(double[6]));
3009
  double (*fine_marg)[6] = (double (*)[6])calloc(graph_blocks, sizeof(double[6]));
@@ -3022,15 +3204,15 @@ static void quantize_tensor_q2k_hpc(const float *weights, int64_t n_elements,
3022
  * PHASE 3 β€” DETERMINISTIC VITERBI DP
3023
  *
3024
  * Replaces the probabilistic beam-search + Born-rule Monte-Carlo
3025
- * shots with an exact, fully-deterministic DP over the 36-state
3026
- * Shor quhit space (6 coarse bins Γ— 6 fine bins).
3027
  *
3028
  * For each graph block i and combined state s = qi_d*6 + qi_m:
3029
  *
3030
  * bin_best_err[i][s] = min candidate error in that (d,m)-bin
3031
  * aggregated over the stride group
3032
- * bin_log_prior[i][s] = log P_coarse(qi_d) + log P_fine(qi_m)
3033
- * from Shor marginals β†’ HPC prior bonus
3034
  *
3035
  * Local Viterbi cost (lower = better):
3036
  * vcost[i][s] = bin_best_err[i][s]
@@ -3098,7 +3280,7 @@ static void quantize_tensor_q2k_hpc(const float *weights, int64_t n_elements,
3098
  }
3099
  }
3100
 
3101
- /* HPC log-prior from Shor marginals */
3102
  for (int v = 0; v < 6; v++) {
3103
  c_tot += coarse_marg[vit_gi][v];
3104
  f_tot += fine_marg [vit_gi][v];
@@ -3364,7 +3546,7 @@ static void quantize_tensor_q2k_hpc(const float *weights, int64_t n_elements,
3364
  * DC residual (written by the sequential Phase 3.9 pre-pass). */
3365
  float dc_adj = (block_dc_bias) ? block_dc_bias[blk] : 0.0f;
3366
 
3367
- /* Adjusted weight view β€” WLS and Shor work on this array;
3368
  * the final error is always reported against the original block_x. */
3369
  float adj_block_x[QK_K];
3370
  {
@@ -3470,7 +3652,7 @@ static void quantize_tensor_q2k_hpc(const float *weights, int64_t n_elements,
3470
  memset(sub_marg, 0, sizeof(sub_marg));
3471
  memset(sub_measured, 0, sizeof(sub_measured));
3472
 
3473
- shor_measure_graph(sg, N_SUB, sub_marg, sub_measured, 1);
3474
 
3475
  for (int j = 0; j < N_SUB; j++) {
3476
  double best_prob = -1.0;
@@ -4334,7 +4516,7 @@ static void quantize_tensor_q2k_hpc(const float *weights, int64_t n_elements,
4334
  fidelity_icon = "β˜…β˜†β˜†β˜†";
4335
  }
4336
 
4337
- printf("\n β”Œβ”€β”€β”€β”€ Shor Measurement Q2_K Report ────────────────────────────────┐\n");
4338
  printf(" β”‚ Elements: %-12lld Blocks: %-12lld β”‚\n",
4339
  (long long)n_elements, (long long)(n_elements / QK_K));
4340
  printf(" β”‚ Weight Οƒ: %-12.4e Range: [%.4e, %.4e] β”‚\n",
@@ -4344,7 +4526,7 @@ static void quantize_tensor_q2k_hpc(const float *weights, int64_t n_elements,
4344
  rmse, rmse_over_sigma);
4345
  printf(" β”‚ Fidelity: %s %-14s β”‚\n",
4346
  fidelity_icon, fidelity_class);
4347
- printf(" β”‚ Engine: Shor Griffiths-Niu (IDFT6 + feed-forward) β”‚\n");
4348
  printf(" β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜\n");
4349
  }
4350
  }
@@ -4656,7 +4838,7 @@ static int write_gguf(const char *output_path, const STMultiFile *mf,
4656
  else fid = "β˜…β˜†β˜†β˜† STD";
4657
 
4658
  if (verbose) {
4659
- printf("\n [Q2_KΒ·Shor] %-47s\n", gguf_names[i]);
4660
  printf(" %10ld elements β†’ %ld bytes Οƒ=%.2e RMSE=%.4e %s\n",
4661
  (long)ti->n_elements,
4662
  (long)(n_blocks * sizeof(BlockQ2K)),
@@ -4731,7 +4913,7 @@ static int write_gguf(const char *output_path, const STMultiFile *mf,
4731
  else fid4 = "β˜…β˜†β˜†β˜† STD";
4732
 
4733
  if (verbose) {
4734
- printf("\n [Q4_0Β·Shor] %-47s\n", gguf_names[i]);
4735
  printf(" %10ld elements β†’ %ld bytes Οƒ=%.2e RMSE=%.4e %s\n",
4736
  (long)ti->n_elements,
4737
  (long)(n_blocks_q4 * sizeof(BlockQ4_0)),
@@ -4820,12 +5002,12 @@ static int write_gguf(const char *output_path, const STMultiFile *mf,
4820
  else { overall_fid = "STANDARD"; overall_icon = "β˜…β˜†β˜†β˜†"; }
4821
 
4822
  printf("\n ╔════════════════════════════════════════════════════════════════╗\n");
4823
- printf(" β•‘ SHOR-OPTIMIZED QUANTIZATION SUMMARY β•‘\n");
4824
  printf(" ╠════════════════════════════════════════════════════════════════╣\n");
4825
  printf(" β•‘ β•‘\n");
4826
- printf(" β•‘ Engine: Griffiths-Niu Sequential Measurement β•‘\n");
4827
- printf(" β•‘ Protocol: IDFT6 β†’ feed-forward β†’ Born β†’ collapse β•‘\n");
4828
- printf(" β•‘ Origin: tesseract_factor.c (Shor's algorithm) β•‘\n");
4829
  printf(" β•‘ β•‘\n");
4830
  printf(" ╠════════════════════════════════════════════════════════════════╣\n");
4831
  printf(" β•‘ Tensors quantized: %-33d β•‘\n", quant_count);
@@ -4988,10 +5170,10 @@ int main(int argc, char **argv)
4988
  printf("\n");
4989
  printf(" ���════════════════════════════════════════════════════════════════╗\n");
4990
  printf(" β•‘ β•‘\n");
4991
- printf(" β•‘ HExState GGUF QUANTIZER v3.0 β€” Shor-Optimized β•‘\n");
4992
  printf(" β•‘ β•‘\n");
4993
  printf(" β•‘ Architecture: HPCGraph Sensitivity Propagation β•‘\n");
4994
- printf(" β•‘ Optimization: Shor's Griffiths-Niu Measurement + iMatrix β•‘\n");
4995
  printf(" β•‘ Output: GGUF v3 (Q2_K, 2.625 bpw) β•‘\n");
4996
  printf(" β•‘ β•‘\n");
4997
  printf(" β•‘ \"The weight and the quantized are opposite faces.\" β•‘\n");
 
19
  * ─────────────────────────────────────────────────
20
  * HPCGraph + CZ edges β†’ Block sensitivity graph
21
  * Complex Amplitude BP β†’ Importance propagation
22
+ * SIEVE sequential selector β†’ Optimal scale search (replaces Shor
23
+ * (log-sieve + parity Griffiths-Niu IDFT6 measurement;
24
+ * back-action) see sieve_measure_graph)
25
  * try_period() validation β†’ Error bound checking
26
  * LLL lattice reduction β†’ (future) Adaptive bit allocation
27
  *
 
650
  *
651
  * Nothing in the live pipeline calls the legacy BP sensitivity graph
652
  * (build_sensitivity_graph + compute_block_error_q2k + SCALE_TABLE) or the
653
+ * llm-compressor MSE grid search (mse_grid_search_q2k_subblock); the sieve /
654
  * Viterbi path superseded them. They are preserved behind this flag instead
655
  * of silently shipping as dead code that still costs an init pass.
656
  * ════════════════════════════════════════════════════════════════════════ */
 
677
  /* ═══════════════════════════════════════════════════════════════════════════
678
  * THREAD-LOCAL HPCGRAPH REUSE β€” Eliminates 776K malloc/free cycles
679
  *
680
+ * The sub-block sieve selection uses a 16-node linear-chain graph that
681
  * is identical in topology every time. Instead of hpc_create()/hpc_destroy()
682
  * inside the OMP hot loop, we reset the same graph to a clean state.
683
  *
 
1211
  * (HPC_W6_RE / HPC_W6_IM) β€” the file-local duplicates were unused. */
1212
  static const double INV_SQRT6 = 0.40824829046386301637; /* 1/√6 */
1213
 
1214
+ /* ── Collapse + Back-Action core (SUPERSEDED by sieve_collapse_site) ──
1215
+ * Kept for reference. Previously: the back-action protocol from Shor's
1216
+ * algorithm for the semi-classical QFT. The sieve path uses real-only
1217
+ * parity back-action instead (no complex CZ phases). */
 
 
 
 
1218
  static void shor_collapse_site(HPCGraph *graph, int target_site, int outcome)
1219
  {
1220
  /* Step 1: Collapse local state to |outcome⟩ */
 
1284
  }
1285
 
1286
  /* ═══════════════════════════════════════════════════════════════════════════
1287
+ * SHOR SEQUENTIAL MEASUREMENT β€” Griffiths-Niu Protocol (SUPERSEDED)
1288
+ * Kept for reference only; all call sites now use sieve_measure_graph.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1289
  * ═══════════════════════════════════════════════════════════════════════════ */
1290
  static void shor_measure_graph(HPCGraph *graph, int64_t n_sites,
1291
  double (*marg_out)[6], int *measured_out,
 
1437
  }
1438
  }
1439
 
1440
+ /* ══════════════════════════════════════════════════════════════��════════════
1441
+ * SIEVE SEQUENTIAL MEASUREMENT (replaces Shor Griffiths-Niu above)
1442
+ *
1443
+ * Ported concepts from sieve.py (SLAB quadratic sieve):
1444
+ * - column-first pass: per-bin progressions, O(sites*degree) real ops,
1445
+ * no per-site complex IDFT6 (cf. sieve 85x fewer trial divisions;
1446
+ * cf. sieve Gamma lesson: no dense BxB matmul on the fast path).
1447
+ * - smoothness filter: keep bins within SIEVE_LOG_SLACK of the best
1448
+ * log-score (cf. sieve.py LOG_SLACK over the log sieve).
1449
+ * - parity back-action: collapsing site k to |outcome> multiplicatively
1450
+ * biases still-unmeasured neighbors AWAY from the same bin
1451
+ * (SIEVE_SAME_PENALTY < 1). Equal-bin runs keep DC/vesica error
1452
+ * in phase; diverse bins cancel in the dot product β€” the real-domain
1453
+ * analogue of sieve parity vectors whose F_2 kernel marks
1454
+ * error-cancelling sets.
1455
+ *
1456
+ * What was dropped vs shor_measure_graph:
1457
+ * - feed-forward phase theta_k (semi-classical QFT correction),
1458
+ * - coherent complex neighbor product C_k(d),
1459
+ * - in-place IDFT6 interference transform,
1460
+ * - complex CZ phase absorption in the collapse.
1461
+ * What was kept: single-pass MSB->LSB order, Boltzmann-encoded locals,
1462
+ * real back-action conditioning, same output signature so the beam
1463
+ * search / Viterbi / sub-block consumers are untouched.
1464
+ * ═══════════════════════════════════════════════════════════════════════════ */
1465
+
1466
+ #define SIEVE_SAME_PENALTY 0.85 /* anti-correlation: same-bin discount */
1467
+ #define SIEVE_LOG_SLACK 1.5 /* log-domain slack, cf. sieve LOG_SLACK */
1468
+
1469
+ static void sieve_collapse_site(HPCGraph *graph, int target_site, int outcome)
1470
+ {
1471
+ /* Step 1: Collapse local state to |outcome> (real one-hot). */
1472
+ for (int v = 0; v < 6; v++) {
1473
+ graph->locals[target_site].edge_re[v] = (v == outcome) ? 1.0 : 0.0;
1474
+ graph->locals[target_site].edge_im[v] = 0.0;
1475
+ }
1476
+ graph->locals[target_site].primary = VIEW_EDGE;
1477
+ graph->locals[target_site].dirty = DIRTY_VERTEX | DIRTY_DIAGONAL | DIRTY_FOLDED;
1478
+ graph->locals[target_site].delta_valid = 0;
1479
+
1480
+ /* Step 2: Real back-action. Neighbor bin d gets multiplied by
1481
+ * SIEVE_SAME_PENALTY when d == outcome, 1.0 otherwise, then the
1482
+ * neighbor is renormalized to unit norm. No complex phases. */
1483
+ HPCAdjList *adj = &graph->adj[target_site];
1484
+ for (uint64_t ei = 0; ei < adj->count; ei++) {
1485
+ uint64_t eid = adj->edge_ids[ei];
1486
+ HPCEdge *edge = &graph->edges[eid];
1487
+ if (edge->fidelity < 0.0) continue; /* skip dead edges */
1488
+ uint64_t partner = (edge->site_a == (uint64_t)target_site) ?
1489
+ edge->site_b : edge->site_a;
1490
+
1491
+ TrialityQuhit *pq = &graph->locals[partner];
1492
+ double norm2 = 0.0;
1493
+ for (int d = 0; d < 6; d++) {
1494
+ double f = (d == outcome) ? SIEVE_SAME_PENALTY : 1.0;
1495
+ pq->edge_re[d] *= f;
1496
+ pq->edge_im[d] *= f;
1497
+ norm2 += pq->edge_re[d] * pq->edge_re[d]
1498
+ + pq->edge_im[d] * pq->edge_im[d];
1499
+ }
1500
+ if (norm2 > 1e-30) {
1501
+ double inv = 1.0 / sqrt(norm2);
1502
+ for (int d = 0; d < 6; d++) {
1503
+ pq->edge_re[d] *= inv;
1504
+ pq->edge_im[d] *= inv;
1505
+ }
1506
+ }
1507
+ pq->dirty = DIRTY_VERTEX | DIRTY_DIAGONAL | DIRTY_FOLDED;
1508
+ pq->delta_valid = 0;
1509
+ }
1510
+
1511
+ /* Step 3: Remove edges touching this site (same bookkeeping as Shor). */
1512
+ for (uint64_t ei = 0; ei < adj->count; ei++) {
1513
+ uint64_t eid = adj->edge_ids[ei];
1514
+ HPCEdge *edge = &graph->edges[eid];
1515
+ uint64_t partner = (edge->site_a == (uint64_t)target_site) ?
1516
+ edge->site_b : edge->site_a;
1517
+
1518
+ HPCAdjList *padj = &graph->adj[partner];
1519
+ for (uint64_t pi = 0; pi < padj->count; pi++) {
1520
+ if (padj->edge_ids[pi] == eid) {
1521
+ padj->edge_ids[pi] = padj->edge_ids[--padj->count];
1522
+ break;
1523
+ }
1524
+ }
1525
+ edge->fidelity = -1.0; /* Mark as dead */
1526
+ }
1527
+ adj->count = 0;
1528
+ }
1529
+
1530
+ /* Drop-in signature match for shor_measure_graph. */
1531
+ static void sieve_measure_graph(HPCGraph *graph, int64_t n_sites,
1532
+ double (*marg_out)[6], int *measured_out,
1533
+ int deterministic)
1534
+ {
1535
+ /* MSB->LSB to preserve downstream order assumptions. */
1536
+ for (int64_t k = n_sites - 1; k >= 0; k--) {
1537
+ int site_k = (int)k;
1538
+
1539
+ /* Step 1: local distribution from |amplitude|^2. */
1540
+ double p0[6], total0 = 0.0;
1541
+ for (int d = 0; d < 6; d++) {
1542
+ p0[d] = graph->locals[site_k].edge_re[d] * graph->locals[site_k].edge_re[d]
1543
+ + graph->locals[site_k].edge_im[d] * graph->locals[site_k].edge_im[d];
1544
+ total0 += p0[d];
1545
+ }
1546
+ if (total0 > 1e-30) {
1547
+ for (int d = 0; d < 6; d++) p0[d] /= total0;
1548
+ } else {
1549
+ for (int d = 0; d < 6; d++) p0[d] = 1.0 / 6.0;
1550
+ }
1551
+
1552
+ /* Step 2: column-first real neighbor bias. For each bin d,
1553
+ * bias[d] = prod over live neighbors of
1554
+ * sum_w q_j[w] * (w == d ? SIEVE_SAME_PENALTY : 1).
1555
+ * Done in log domain so the slack filter is exact. */
1556
+ double log_score[6];
1557
+ for (int d = 0; d < 6; d++)
1558
+ log_score[d] = log(p0[d] + 1e-30);
1559
+
1560
+ const HPCAdjList *adj = &graph->adj[site_k];
1561
+ for (uint64_t ei = 0; ei < adj->count; ei++) {
1562
+ uint64_t eid = adj->edge_ids[ei];
1563
+ const HPCEdge *edge = &graph->edges[eid];
1564
+ if (edge->fidelity < 0.0) continue;
1565
+ uint64_t partner = (edge->site_a == (uint64_t)site_k) ?
1566
+ edge->site_b : edge->site_a;
1567
+
1568
+ const TrialityQuhit *pq = &graph->locals[partner];
1569
+ double q[6], qt = 0.0;
1570
+ for (int w = 0; w < 6; w++) {
1571
+ q[w] = pq->edge_re[w] * pq->edge_re[w]
1572
+ + pq->edge_im[w] * pq->edge_im[w];
1573
+ qt += q[w];
1574
+ }
1575
+ if (qt > 1e-30) {
1576
+ for (int w = 0; w < 6; w++) q[w] /= qt;
1577
+ } else {
1578
+ for (int w = 0; w < 6; w++) q[w] = 1.0 / 6.0;
1579
+ }
1580
+ for (int d = 0; d < 6; d++) {
1581
+ double mix = 0.0;
1582
+ for (int w = 0; w < 6; w++)
1583
+ mix += q[w] * ((w == d) ? SIEVE_SAME_PENALTY : 1.0);
1584
+ log_score[d] += log(mix + 1e-30);
1585
+ }
1586
+ }
1587
+
1588
+ /* Step 3: smoothness filter β€” bins further than SIEVE_LOG_SLACK
1589
+ * below the best log-score get zeroed (sieve LOG_SLACK analog). */
1590
+ double best = log_score[0];
1591
+ for (int d = 1; d < 6; d++)
1592
+ if (log_score[d] > best) best = log_score[d];
1593
+
1594
+ double probs[6], total = 0.0;
1595
+ for (int d = 0; d < 6; d++) {
1596
+ if (best - log_score[d] > SIEVE_LOG_SLACK)
1597
+ probs[d] = 0.0;
1598
+ else
1599
+ probs[d] = exp(log_score[d] - best);
1600
+ total += probs[d];
1601
+ }
1602
+ if (total > 1e-30) {
1603
+ for (int d = 0; d < 6; d++) probs[d] /= total;
1604
+ } else {
1605
+ for (int d = 0; d < 6; d++) probs[d] = 1.0 / 6.0;
1606
+ }
1607
+
1608
+ for (int v = 0; v < 6; v++)
1609
+ marg_out[k][v] = probs[v];
1610
+
1611
+ /* Step 4: outcome selection (MAP for quantization). */
1612
+ int outcome;
1613
+ if (deterministic) {
1614
+ outcome = 0;
1615
+ double max_p = probs[0];
1616
+ for (int v = 1; v < 6; v++) {
1617
+ if (probs[v] > max_p) { max_p = probs[v]; outcome = v; }
1618
+ }
1619
+ } else {
1620
+ static unsigned int sieve_rng = 271828;
1621
+ sieve_rng = sieve_rng * 1664525u + 1013904223u;
1622
+ double r01 = (double)(sieve_rng >> 8) / 16777216.0;
1623
+ double cumul = 0.0;
1624
+ outcome = 5;
1625
+ for (int v = 0; v < 6; v++) {
1626
+ cumul += probs[v];
1627
+ if (r01 <= cumul) { outcome = v; break; }
1628
+ }
1629
+ }
1630
+
1631
+ measured_out[k] = outcome;
1632
+
1633
+ /* Step 5: real collapse + back-action. */
1634
+ sieve_collapse_site(graph, site_k, outcome);
1635
+ }
1636
+ }
1637
+
1638
  /* ═══════════════════════════════════════════════════════════════════════════
1639
  * HPC-OPTIMIZED Q4_0 QUANTIZATION (for attention tensors)
1640
  *
 
1948
  for (int64_t i = 0; i < graph_blocks - 1; i++)
1949
  hpc_cz(graph, i, i + 1);
1950
 
1951
+ /* ── Sieve sequential selection ──
1952
+ * Replaces Shor/BP with real-domain log-sieve + parity
1953
+ * back-action (see sieve_measure_graph).
1954
  * Single pass, no iteration, no message damping. */
1955
  double (*marg)[6] = (double (*)[6])calloc(graph_blocks, sizeof(double[6]));
1956
  int *shor_measured = (int *)calloc(graph_blocks, sizeof(int));
1957
 
1958
+ sieve_measure_graph(graph, graph_blocks, marg, shor_measured, 1);
1959
 
1960
  free(shor_measured);
1961
 
 
2430
  }
2431
 
2432
  /* ════════════════════════════════════════════════════════════════════════
2433
+ * Q8_0 HPC QUANTIZER β€” Sieve pipeline at 8 bits
2434
  *
2435
  * Same pipeline as Q4_0: WLS scale + tight candidate grid scored on the
2436
  * extended objective (weighted SSE + DC + vesica/wave), triality-quhit
2437
  * graph with Boltzmann-encoded candidate errors, CZ chain entanglement,
2438
+ * sieve sequential selection for bin consensus, greedy
2439
  * override (HEX_GREEDY_OVERRIDE_RATIO), then per-block ULP polish, the
2440
  * vesica/DC error-shaping descent with an extended-objective guard, and
2441
  * the candidate floor. Intended for embedding / LM-head tensors (tied
 
2545
  best_candidate[blk] = 11; /* Γ—1.0000 neutral seed */
2546
  }
2547
 
2548
+ /* ── Phase 3: Sieve graph β€” sequential selector, CZ chain ── */
2549
  int shor_ran = 0;
2550
  if (n_blocks >= 2) {
2551
  int64_t graph_blocks = (n_blocks > 200) ? 200 : n_blocks;
 
2617
  double (*marg)[6] = (double (*)[6])calloc(graph_blocks, sizeof(double[6]));
2618
  int *measured = (int *)calloc(graph_blocks, sizeof(int));
2619
  if (marg && measured) {
2620
+ sieve_measure_graph(graph, graph_blocks, marg, measured, 1);
2621
 
2622
+ /* Per-block selection: best candidate inside the sieve-
2623
  * measured bin, then greedy override against the global
2624
  * argmin β€” identical Step-F semantics to Q2_K/Q4_0. */
2625
  for (int64_t i = 0; i < graph_blocks; i++) {
 
3062
  }
3063
 
3064
  /* ══════════════════════════════════════════════════════════════════
3065
+ * PHASE 3: HPC Graph β€” Sieve Sequential Selection
3066
  * ══════════════════════════════════════════════════════════════════ */
3067
 
3068
  int *best_candidate = (int *)malloc(n_blocks * sizeof(int));
 
3185
  double (*shor_marg)[6] = (double (*)[6])calloc(n_sites, sizeof(double[6]));
3186
  int *shor_measured = (int *)calloc(n_sites, sizeof(int));
3187
 
3188
+ sieve_measure_graph(graph, n_sites, shor_marg, shor_measured, 1);
3189
 
3190
  double (*coarse_marg)[6] = (double (*)[6])calloc(graph_blocks, sizeof(double[6]));
3191
  double (*fine_marg)[6] = (double (*)[6])calloc(graph_blocks, sizeof(double[6]));
 
3204
  * PHASE 3 β€” DETERMINISTIC VITERBI DP
3205
  *
3206
  * Replaces the probabilistic beam-search + Born-rule Monte-Carlo
3207
+ * shots with an exact, fully-deterministic DP over the 36-state
3208
+ * sieve quhit space (6 coarse bins Γ— 6 fine bins).
3209
  *
3210
  * For each graph block i and combined state s = qi_d*6 + qi_m:
3211
  *
3212
  * bin_best_err[i][s] = min candidate error in that (d,m)-bin
3213
  * aggregated over the stride group
3214
+ * bin_log_prior[i][s] = log P_coarse(qi_d) + log P_fine(qi_m)
3215
+ * from sieve marginals β†’ HPC prior bonus
3216
  *
3217
  * Local Viterbi cost (lower = better):
3218
  * vcost[i][s] = bin_best_err[i][s]
 
3280
  }
3281
  }
3282
 
3283
+ /* HPC log-prior from sieve marginals */
3284
  for (int v = 0; v < 6; v++) {
3285
  c_tot += coarse_marg[vit_gi][v];
3286
  f_tot += fine_marg [vit_gi][v];
 
3546
  * DC residual (written by the sequential Phase 3.9 pre-pass). */
3547
  float dc_adj = (block_dc_bias) ? block_dc_bias[blk] : 0.0f;
3548
 
3549
+ /* Adjusted weight view β€” WLS and sieve work on this array;
3550
  * the final error is always reported against the original block_x. */
3551
  float adj_block_x[QK_K];
3552
  {
 
3652
  memset(sub_marg, 0, sizeof(sub_marg));
3653
  memset(sub_measured, 0, sizeof(sub_measured));
3654
 
3655
+ sieve_measure_graph(sg, N_SUB, sub_marg, sub_measured, 1);
3656
 
3657
  for (int j = 0; j < N_SUB; j++) {
3658
  double best_prob = -1.0;
 
4516
  fidelity_icon = "β˜…β˜†β˜†β˜†";
4517
  }
4518
 
4519
+ printf("\n β”Œβ”€β”€β”€β”€ Sieve Selection Q2_K Report ──────────────────────────────────┐\n");
4520
  printf(" β”‚ Elements: %-12lld Blocks: %-12lld β”‚\n",
4521
  (long long)n_elements, (long long)(n_elements / QK_K));
4522
  printf(" β”‚ Weight Οƒ: %-12.4e Range: [%.4e, %.4e] β”‚\n",
 
4526
  rmse, rmse_over_sigma);
4527
  printf(" β”‚ Fidelity: %s %-14s β”‚\n",
4528
  fidelity_icon, fidelity_class);
4529
+ printf(" β”‚ Engine: Sieve sequential (log-sieve + parity) β”‚\n");
4530
  printf(" β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜\n");
4531
  }
4532
  }
 
4838
  else fid = "β˜…β˜†β˜†β˜† STD";
4839
 
4840
  if (verbose) {
4841
+ printf("\n [Q2_KΒ·Sieve] %-47s\n", gguf_names[i]);
4842
  printf(" %10ld elements β†’ %ld bytes Οƒ=%.2e RMSE=%.4e %s\n",
4843
  (long)ti->n_elements,
4844
  (long)(n_blocks * sizeof(BlockQ2K)),
 
4913
  else fid4 = "β˜…β˜†β˜†β˜† STD";
4914
 
4915
  if (verbose) {
4916
+ printf("\n [Q4_0Β·Sieve] %-47s\n", gguf_names[i]);
4917
  printf(" %10ld elements β†’ %ld bytes Οƒ=%.2e RMSE=%.4e %s\n",
4918
  (long)ti->n_elements,
4919
  (long)(n_blocks_q4 * sizeof(BlockQ4_0)),
 
5002
  else { overall_fid = "STANDARD"; overall_icon = "β˜…β˜†β˜†β˜†"; }
5003
 
5004
  printf("\n ╔════════════════════════════════════════════════════════════════╗\n");
5005
+ printf(" β•‘ SIEVE-OPTIMIZED QUANTIZATION SUMMARY β•‘\n");
5006
  printf(" ╠════════════════════════════════════════════════════════════════╣\n");
5007
  printf(" β•‘ β•‘\n");
5008
+ printf(" β•‘ Engine: Sieve Sequential Selection β•‘\n");
5009
+ printf(" β•‘ Protocol: log-sieve β†’ slack filter β†’ parity collapse β•‘\n");
5010
+ printf(" β•‘ Origin: sieve.py (SLAB quadratic sieve) β•‘\n");
5011
  printf(" β•‘ β•‘\n");
5012
  printf(" ╠════════════════════════════════════════════════════════════════╣\n");
5013
  printf(" β•‘ Tensors quantized: %-33d β•‘\n", quant_count);
 
5170
  printf("\n");
5171
  printf(" ���════════════════════════════════════════════════════════════════╗\n");
5172
  printf(" β•‘ β•‘\n");
5173
+ printf(" β•‘ HExState GGUF QUANTIZER v3.0 β€” Sieve-Optimized β•‘\n");
5174
  printf(" β•‘ β•‘\n");
5175
  printf(" β•‘ Architecture: HPCGraph Sensitivity Propagation β•‘\n");
5176
+ printf(" β•‘ Optimization: Sieve Sequential Selection + iMatrix β•‘\n");
5177
  printf(" β•‘ Output: GGUF v3 (Q2_K, 2.625 bpw) β•‘\n");
5178
  printf(" β•‘ β•‘\n");
5179
  printf(" β•‘ \"The weight and the quantized are opposite faces.\" β•‘\n");
hexstate_requantize.py CHANGED
@@ -70,7 +70,7 @@ def _load_hexstate_lib():
70
  ctypes.c_int, # verbose
71
  ]
72
 
73
- # Q8_0 HPC quantizer (Shor pipeline; tied embeddings / LM head)
74
  if hasattr(lib, 'hexstate_quantize_tensor_q8_0_hpc'):
75
  lib.hexstate_quantize_tensor_q8_0_hpc.restype = None
76
  lib.hexstate_quantize_tensor_q8_0_hpc.argtypes = [
@@ -657,7 +657,7 @@ def should_quantize(name, n_dims, dims, tied_embeddings=False):
657
  - altup, laurel β€” small Gemma-specific tensors
658
  - token_embd.weight / output.weight β€” always excluded here.
659
  When embeddings are TIED, main() routes token_embd.weight to
660
- Q8_0 (HPC Shor pipeline) instead: the same tensor serves as both
661
  embedding lookup AND LM head, and Q2_K/Q4_0 there destroys logit
662
  precision β†’ looping / repetitive generation. --keep-embd keeps
663
  it at source precision instead.
@@ -819,7 +819,7 @@ def main():
819
  if keep_embd:
820
  print(" ⚠ Tied embeddings detected β€” token_embd.weight kept at source precision (--keep-embd)")
821
  else:
822
- print(" ⚠ Tied embeddings detected β€” token_embd.weight β†’ Q8_0 via Shor pipeline (serves as LM head;")
823
  print(" Q2_K/Q4_0 here destroys logit precision β€” classic looping-output symptom)")
824
  print()
825
 
@@ -839,7 +839,7 @@ def main():
839
  # code (should_quantize always returned False for
840
  # token_embd), so the tensor was silently kept at F16/BF16.
841
  # Now: Q8_0 (8.5 bpw, ~2x smaller than F16) via the HPC
842
- # Shor pipeline β€” transparent for both embedding lookup
843
  # and logit projection.
844
  will_quant = 'EMBD_Q8'
845
  total_embd += 1
@@ -867,7 +867,7 @@ def main():
867
  else:
868
  print(f" Tensors to quantize (Q2_K): {total_quant}")
869
  print(f" Tensors to promote (Q4_0Β·HPC): {total_attn}")
870
- print(f" Tied embd β†’ Q8_0 (ShorΒ·HPC): {total_embd}")
871
  print(f" Tensors to keep as-is: {total_keep}")
872
  print()
873
 
@@ -885,7 +885,7 @@ def main():
885
  out_type = GGML_TYPE_Q8_0
886
  n_blocks = ti['n_elements'] // 32
887
  out_size = n_blocks * 34
888
- print(f" [EMBD→Q8_0·Shor] {ti['name']} ({ti['n_elements']:,} elements)")
889
  elif quant_plan[i] == 'ATTN_Q4':
890
  # Attention tensor β†’ Q4_0 HPC (4.5 bpw)
891
  out_type = GGML_TYPE_Q4_0
@@ -1077,7 +1077,7 @@ def main():
1077
  raw_data = fin.read(ti['data_size'])
1078
 
1079
  if quant_plan[i] == 'EMBD_Q8':
1080
- # ── Tied embedding β†’ Q8_0 via the HPC Shor pipeline ──
1081
  if ti['type'] == GGML_TYPE_BF16:
1082
  f32 = bf16_to_f32(raw_data, ti['n_elements'])
1083
  elif ti['type'] == GGML_TYPE_F16:
@@ -1118,7 +1118,7 @@ def main():
1118
  )
1119
  fout.write(output_buf.tobytes())
1120
  rmse8 = float(np.sqrt(error.value / max(n_el, 1)))
1121
- print(f"\n [Q8_0Β·Shor] {ti['name']} RMSE={rmse8:.6e}")
1122
  else:
1123
  q8_bytes, n_blocks_q8, sse8 = quantize_tensor_q8_0(f32)
1124
  fout.write(q8_bytes)
 
70
  ctypes.c_int, # verbose
71
  ]
72
 
73
+ # Q8_0 HPC quantizer (Sieve pipeline; tied embeddings / LM head)
74
  if hasattr(lib, 'hexstate_quantize_tensor_q8_0_hpc'):
75
  lib.hexstate_quantize_tensor_q8_0_hpc.restype = None
76
  lib.hexstate_quantize_tensor_q8_0_hpc.argtypes = [
 
657
  - altup, laurel β€” small Gemma-specific tensors
658
  - token_embd.weight / output.weight β€” always excluded here.
659
  When embeddings are TIED, main() routes token_embd.weight to
660
+ Q8_0 (HPC sieve pipeline) instead: the same tensor serves as both
661
  embedding lookup AND LM head, and Q2_K/Q4_0 there destroys logit
662
  precision β†’ looping / repetitive generation. --keep-embd keeps
663
  it at source precision instead.
 
819
  if keep_embd:
820
  print(" ⚠ Tied embeddings detected β€” token_embd.weight kept at source precision (--keep-embd)")
821
  else:
822
+ print(" ⚠ Tied embeddings detected β€” token_embd.weight β†’ Q8_0 via sieve pipeline (serves as LM head;")
823
  print(" Q2_K/Q4_0 here destroys logit precision β€” classic looping-output symptom)")
824
  print()
825
 
 
839
  # code (should_quantize always returned False for
840
  # token_embd), so the tensor was silently kept at F16/BF16.
841
  # Now: Q8_0 (8.5 bpw, ~2x smaller than F16) via the HPC
842
+ # Sieve pipeline β€” transparent for both embedding lookup
843
  # and logit projection.
844
  will_quant = 'EMBD_Q8'
845
  total_embd += 1
 
867
  else:
868
  print(f" Tensors to quantize (Q2_K): {total_quant}")
869
  print(f" Tensors to promote (Q4_0Β·HPC): {total_attn}")
870
+ print(f" Tied embd β†’ Q8_0 (SieveΒ·HPC): {total_embd}")
871
  print(f" Tensors to keep as-is: {total_keep}")
872
  print()
873
 
 
885
  out_type = GGML_TYPE_Q8_0
886
  n_blocks = ti['n_elements'] // 32
887
  out_size = n_blocks * 34
888
+ print(f" [EMBD→Q8_0·Sieve] {ti['name']} ({ti['n_elements']:,} elements)")
889
  elif quant_plan[i] == 'ATTN_Q4':
890
  # Attention tensor β†’ Q4_0 HPC (4.5 bpw)
891
  out_type = GGML_TYPE_Q4_0
 
1077
  raw_data = fin.read(ti['data_size'])
1078
 
1079
  if quant_plan[i] == 'EMBD_Q8':
1080
+ # ── Tied embedding β†’ Q8_0 via the HPC sieve pipeline ──
1081
  if ti['type'] == GGML_TYPE_BF16:
1082
  f32 = bf16_to_f32(raw_data, ti['n_elements'])
1083
  elif ti['type'] == GGML_TYPE_F16:
 
1118
  )
1119
  fout.write(output_buf.tobytes())
1120
  rmse8 = float(np.sqrt(error.value / max(n_el, 1)))
1121
+ print(f"\n [Q8_0Β·Sieve] {ti['name']} RMSE={rmse8:.6e}")
1122
  else:
1123
  q8_bytes, n_blocks_q8, sse8 = quantize_tensor_q8_0(f32)
1124
  fout.write(q8_bytes)