From 3ee789bb7b38c61ae9f4e46070191871c83404fb Mon Sep 17 00:00:00 2001
From: wakesync <60821182+wakesync@users.noreply.github.com>
Date: Sun, 29 Nov 2026 02:01:39 +0000
Subject: [PATCH 01/11] research(#214): PREREG fold constants by the rule
(first commit, alone)
---
.../20261129-214-fold-constants/PREREG.md | 65 +++++++++++++++++++
1 file changed, 65 insertions(+)
create mode 100644 experiments/20261129-214-fold-constants/PREREG.md
diff --git a/experiments/20261129-214-fold-constants/PREREG.md b/experiments/20261129-214-fold-constants/PREREG.md
new file mode 100644
index 00000000..5ad756cf
--- /dev/null
+++ b/experiments/20261129-214-fold-constants/PREREG.md
@@ -0,0 +1,65 @@
+# PREREG: 20261129-214-fold-constants
+
+Issue: #214. Lane: fold214. Date (UTC): 2026-11-29 02:00. Base commit: cdf78449d390a1f62dc77d28c5cf6cb8c0bb1b02.
+
+CPU only. No weights, no GPU, no CUDA file is edited or compiled in this step.
+
+## The rule, quoted from main
+
+`scales/README.md` line 541 to 543 (the issue cites 542): "The conversion ([../docs/model.md](../docs/model.md), section 11.3) multiplies a norm's gains and the constant sqrt(d) / 64 into every matrix that reads the norm's output, and 1 / sqrt(dh) into the query matrix."
+
+`scales/README.md` lines 594 to 601: "Let t be the largest integer with 4^t <= d, and c = sqrt(d / 4^t), so that sqrt(d) = c * 2^t and 1 <= c < 2; at d = 5,120, t = 6 and c = sqrt(1.25). [...] In a product with a weight matrix the conversion's constant sqrt(d) / 64 cancels c only where t = 6. So the shifts below and E_logit are right for widths from 4,096 to 16,383 only; the conversion accepts only 5,120."
+
+Check of the issue's question "is `sqrt(d) / 64` only the t = 6 case of `c`": sqrt(d) / 64 = c * 2^t / 2^6, which equals c exactly when t = 6, that is for 4,096 <= d < 16,384. For other widths the README's own text says the constant sqrt(d) / 64 does not cancel c. So the general constant that keeps the norm's output scale of step 2 is c itself, and the rule under test is:
+
+- t = the largest integer with 4^t <= d
+- c = sqrt(d) / 2^t = sqrt(d / 4^t), with 1 <= c < 2
+- c_0 = RN32(c) (the norm constant)
+- c_q = RN32(c / sqrt(dh)) = RN32(sqrt(d / (4^t dh))) (the query constant)
+
+RN32 is the binary32 number nearest to the exact real value, ties to even, applied ONCE to the exact real (not RN32(RN32(c) / sqrt(dh)) and not a rounding of a binary64 intermediate).
+
+## Hypothesis
+
+The rule above, computed exactly, gives the bit patterns 0x3F8F1BBD (c_0) and 0x3DCA62C2 (c_q) at (d, dh) = (5,120, 128), the literals on main, so `weight_sums` built with `fold_constants(width, head)` in place of the literals writes byte-identical sums files on every input of its existing test suite.
+
+## Expected words
+
+- (5,120, 128): t = 6, c = sqrt(1.25). Expected c_0 = 0x3F8F1BBD, c_q = 0x3DCA62C2 (main's literals).
+- (5,120, 256): t = 6, c_0 unchanged (0x3F8F1BBD); c_q = RN32(sqrt(1.25) / 16). Because 16 is a power of two, c_q is expected to be c_0 with its exponent lowered by 4, 0x3D8F1BBD. This is a prediction written before any computation; the independent computation decides it.
+
+## Metric
+
+1. The two 32-bit words returned by the new Python `fold_constants(width, head)` (in `weight_sums/weight_sums.py`) and by its C++ twin (in `weight_sums/weight_sums.cpp`) at (5,120, 128) and (5,120, 256).
+2. The same words from an independent computation: mpmath at 256 bits of precision (binary search of the nearest binary32 on the mpf value, ties checked) and, separately, exact rational arithmetic (integer square roots), in a script under this directory. Both results committed in a values file.
+3. Byte comparison (`cmp`) of sums files written by `bin/weight_sums` and `weight_sums.py` built from the base commit and from the branch head on the suite's own inputs: oracle seeds 1, 2, 3, 4, 100, 101, 102 at e_res 11, seed 1 at e_res 10, seed 2 with `--decode-twice` (Python), and the `make_model` model, plus the pass/fail line of every step of `weight_sums/tests/run_tests.sh` at base and at head.
+
+## Acceptance threshold
+
+PASS, item by item:
+- P1: Python and C++ `fold_constants(5120, 128)` both return exactly (0x3F8F1BBD, 0x3DCA62C2).
+- P2: the independent computation (both methods) gives the same words at (5,120, 128) as P1, and both methods agree with Python and C++ at (5,120, 256).
+- P3: every sums file of metric 3 at head equals the base's byte for byte, for both programs; and `run_tests.sh` at head has the same set of passed steps as at base (no step passes at base and fails at head).
+- P4: new unit tests (Python and C++) pass: the literals at (5,120, 128), the recorded values at (5,120, 256), and agreement of Python and C++ on a fixed list of further shapes.
+
+## Kill criterion
+
+- Negative result, stop at once and change nothing further: the exact rule does not give 0x3F8F1BBD and 0x3DCA62C2 at (5,120, 128), or any byte of any sums file of metric 3 differs between base and head. Report the difference.
+- Stop and report: the Python and C++ CPU parts cannot change without a CUDA file changing in the same step (for example a shared header).
+- Hard stop 2026-11-29T04:20Z (150 minutes after the lane's first STATE line at 01:50Z). Whatever is done by then is committed and written up as partial.
+
+## Runs
+
+The outputs are deterministic functions of their inputs (integer and exact arithmetic, sums files hashed), so variance is not a property of interest: each comparison is run once per arm (base, head), and a single differing byte is a FAIL. Order: base arm first, then head arm, same host, same compiler (g++ 13.3.0), same Python environment. Cores 14-15 (`taskset -c 14-15`). The registered suite (metric 3, run_tests.sh) is not run at any size before the code is complete; development debugging uses oracle seed 7 at e_res 11, which the suite does not use, and the shapes of P4. Nothing excluded.
+
+## Scope cut (by the coordinator, before registration)
+
+This host has no GPU. The CUDA reader (`prover/weights_commitment/fold_weights.cuh`, `reads()`) and its shape gate (`prover/weights_commitment/fold_source.cuh`, `FoldSource::open`) are NOT changed; RESULTS.md will state the exact change they need as an open item, not made and not compiled. The shape gates in `weight_sums.py` and `weight_sums.cpp` that accept only (5,120, 128) also stay as they are in this step: opening them while the CUDA side still refuses every other shape would make `weight_sums` write files the prover cannot use, and no admission gate is opened here. The constants are computed by the rule; the shapes admitted are unchanged.
+
+## Prior art
+
+The rule is the repository's own (`scales/README.md`, `docs/model.md` section 11.3). Folding a norm's gain and sqrt(width) into the next matrix: QuaRot (arXiv:2404.00456) and SliceGPT (arXiv:2401.15024), as cited in `weight_sums.py`. Correctly rounded square roots in binary32: IEEE 754-2019 requires sqrt to be correctly rounded; here the operand d / (4^t dh) is rational, so the exact rounding is done with integer square roots instead of a library call. The issue names a public fork (marcellobardus/FlashProve-TinyLlama1.1B, commit c0eb59e07283) that reports the same change for TinyLlama; it was not read for this step and no code is taken from it. What is new here: an exact computation of both words for any width and head size, checked against main's literals, and the values for dh = 256.
+
+## Amendments
+
+(empty at registration)
From 38413939ca8328c24d51ddcd2560f3c2bbd56558 Mon Sep 17 00:00:00 2001
From: wakesync <60821182+wakesync@users.noreply.github.com>
Date: Sun, 29 Nov 2026 02:09:15 +0000
Subject: [PATCH 02/11] research(#214): PREREG amendment A1 (before registered
runs)
---
experiments/20261129-214-fold-constants/PREREG.md | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/experiments/20261129-214-fold-constants/PREREG.md b/experiments/20261129-214-fold-constants/PREREG.md
index 5ad756cf..a3ab725f 100644
--- a/experiments/20261129-214-fold-constants/PREREG.md
+++ b/experiments/20261129-214-fold-constants/PREREG.md
@@ -62,4 +62,11 @@ The rule is the repository's own (`scales/README.md`, `docs/model.md` section 11
## Amendments
-(empty at registration)
+### A1 (2026-11-29 02:12 UTC, after code and the seed-7 debug run, BEFORE any registered run)
+Concrete choices the code makes that the registration did not fix. None changes a threshold, an input or a pass rule.
+- Python environment for both arms: a fresh venv with numpy 2.3.5, gguf 0.19.0 (the versions `weight_sums.py` names) and mpmath 1.3.0; the host's system python has no gguf, so `run_tests.sh` would otherwise skip every Python step.
+- Metric 3 is run by `compare_base_head.py` in this directory: base checkout = a detached worktree at the base commit with its own `bin/weight_sums`, head checkout = the branch with its own. Inputs are written once by the head's `tests/oracle.py` (unchanged by this PR) and by one `make_model` built from the head's `prover/` (unchanged by this PR). Each sums file is also compared with the expected file of oracle.py / make_model. Output `runs/compare.json`.
+- `run_tests.sh` is run once at the base and once at the head, each with the venv first on PATH, output `runs/run_tests.base.txt` and `runs/run_tests.head.txt`. The head adds one step (`fold-constants.py`), so P3's "same set of passed steps" means: every step passed at base also passes at head.
+- `values.json` (metric 2) was written by `independent_values.py` before this amendment; it is not a run of the registered suite. P4's C++ test pins the (5,120, 256) words of values.json and, at five further shapes, words that `fold_constants_test.py` checks for Python against an independent bisection.
+- C++: `fold_constants` is `constexpr` with a `static_assert` that it gives main's literals at (5,120, 128), so a build that would change them does not compile. The shape gates of both programs are unchanged (PREREG "Scope cut").
+- Debug: one comparison on oracle seed 7, e_res 11 (not a registered input), both languages, base vs head: byte-identical. Output not kept as evidence.
From 92e1dc1b1609a92d35c594b72ffb8cd79af6bae9 Mon Sep 17 00:00:00 2001
From: wakesync <60821182+wakesync@users.noreply.github.com>
Date: Sun, 29 Nov 2026 02:09:28 +0000
Subject: [PATCH 03/11] research(#214): fold_constants(width, head) in
weight_sums.py and weight_sums.cpp, tests, independent values
---
.../compare_base_head.py | 105 ++++++++++++++++
.../independent_values.py | 118 ++++++++++++++++++
.../20261129-214-fold-constants/values.json | 44 +++++++
weight_sums/README.md | 6 +-
weight_sums/tests/fold_constants_test.py | 100 +++++++++++++++
weight_sums/tests/run_tests.sh | 3 +
weight_sums/tests/unit_test.cpp | 28 +++++
weight_sums/weight_sums.cpp | 61 ++++++++-
weight_sums/weight_sums.py | 62 ++++++++-
9 files changed, 518 insertions(+), 9 deletions(-)
create mode 100644 experiments/20261129-214-fold-constants/compare_base_head.py
create mode 100644 experiments/20261129-214-fold-constants/independent_values.py
create mode 100644 experiments/20261129-214-fold-constants/values.json
create mode 100644 weight_sums/tests/fold_constants_test.py
diff --git a/experiments/20261129-214-fold-constants/compare_base_head.py b/experiments/20261129-214-fold-constants/compare_base_head.py
new file mode 100644
index 00000000..faef2545
--- /dev/null
+++ b/experiments/20261129-214-fold-constants/compare_base_head.py
@@ -0,0 +1,105 @@
+#!/usr/bin/env python3
+# Copyright 2026 Anthropic PBC
+# SPDX-License-Identifier: Apache-2.0
+
+"""PREREG metric 3: byte comparison of the sums files that weight_sums writes at the base commit and at the head.
+
+usage: python3 compare_base_head.py [--debug]