Skip to content

Round underflowing leaves in the precision reference instead of raising - #43

Merged
SamMausberg merged 5 commits into
mainfrom
kernel/precision-subnormal
Sep 30, 2026
Merged

SamMausberg merged 5 commits into
mainfrom
kernel/precision-subnormal

Conversation

@SamMausberg

Copy link
Copy Markdown
Owner

Handles the Codex review comment on PR #7 (src/precision_reference.py:250): Accumulator.sum raised ValueError when a product of two valid subnormal BF16 operands was not representable in FP32 (for example 2^-133 * 2^-133), so an exact tie between two such rows made certify_argmax fail instead of refining. The comment's advice was sound and is taken as given.

Change

A product of two BF16 values has at most 16 significant bits, so it is inexact in FP32 only below the normal range. Such a leaf is now rounded into the accumulator format the way the modelled accumulator would round it: gradual underflow, or flush to zero when ftz is set. Its error is at most Accumulator.underflow, and error_bound already charges one underflow per leaf through nodes(n) = 2n + splits, so the bound itself is unchanged. A leaf that is inexact in the normal range is still rejected as an input error.

Validation

  • New test_subnormal_products: subnormal weights and hidden states, FP32 ladder with and without flush to zero. Every rescoring interval must enclose the exact logit, and the certified token must equal the exact argmax. Exact ties between duplicate rows (71 of 160 cases) must resolve to the smallest index.
  • The test fails on the previous code with the reported ValueError.
  • python tests/test_precision.py passes all 21 test methods and regenerates evidence/precision/precision_tests.json.
  • pre-commit passes on the changed files.

GPU counterpart

The certified head's GPU bound code, on branch kernel/certified-head, was checked for the same case:

  • Its rescoring forms BF16 products in FP64, where they are exact.
  • Its approximate-pass envelope already carries an absolute 2^-100 term, which covers flush to zero of our int8 pass and of the stock kernel.
  • The rescoring radius lacked that absolute term for the stock kernel's own FP32 underflow. It now adds 2^-100, which covers flush to zero of up to 2K products at 2^-126 each. That change ships with the kernel PR.

Accumulator.sum rejected any leaf that is not exact in the accumulator
format. Products of two BF16 values have at most 16 significant bits, so in
FP32 that only happens below the normal range, for example 2^-133 * 2^-133.
Rescoring two such rows (an exact tie between duplicate rows with subnormal
operands) therefore made certify_argmax raise instead of refining.

A leaf below the normal range is now rounded into the format as the modelled
accumulator would (gradual underflow, or flush to zero when ftz is set). Its
error is at most `underflow`, which error_bound already charges once per leaf
through nodes(n) = 2n + splits, so the bound is unchanged. A leaf that is
inexact in the normal range is still an input error.

tests/test_precision.py adds test_subnormal_products: subnormal weights and
hidden states, with and without flush to zero, checks that every rescoring
interval encloses the exact logit and that exact ties between identical rows
resolve to the smallest index (71 ties in 160 cases). The test fails on the
previous code. evidence/precision/precision_tests.json is regenerated by
`python tests/test_precision.py` (all 21 test methods pass).
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-30T21:22:47.585293Z c7046f2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c7046f2480

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/precision_reference.py Outdated
raise ValueError('accumulated leaves must be exact in the accumulator format')
if abs(t) >= self.fmt.min_normal:
raise ValueError('accumulated leaves must be exact in the accumulator format')
t = self._r(t)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve subnormal products until the FMA rounds

When order='sequential' models the documented FMA/add chain with ftz=False, rounding each leaf here inserts an extra rounding before the fused addition. For example, the valid exact BF16 products [2^-149, 2^-150] should produce 2^-148: after the first term, adding the half-ulp second term rounds the tie to the even 2^-148; this implementation first rounds 2^-150 to zero and instead returns 2^-149. Thus rescoring and stock_logits no longer emulate the selected accumulator for these inputs; retain the exact product until the accumulator operation rounds it.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, and fixed in e690786. The source change is committed first (a5334a1), and the bundle was regenerated at that commit with python tests/test_precision.py 2>&1 | tee evidence/precision/precision_tests.log. The JSON records repo_commit a5334a1. The log and README now match: 21 test methods, 40,015 checks. Relative to main, the only changed count is crossover_implications (1536 to 1559), because the new test consumes the shared seeded stream; the README does not cite it.

Comment thread evidence/precision/precision_tests.json Outdated
"test_methods": 21,
"seed": 20260930,
"repo_commit": "8296b585caa5352ad061fff1feb9ecdae920127d",
"repo_commit": "25278b6cb149799596118f6b818c0dc81b693a24",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Tie the regenerated evidence to the tested source

The recorded revision 25278b6 is this commit's parent, which contains neither test_subnormal_products nor the new underflow behavior, so checking out the claimed repo_commit cannot reproduce this 21-test report. The companion precision_tests.log and README also remain at the prior 20-test/8296b585 run, leaving the checked-in evidence bundle with contradictory provenance; regenerate the complete bundle after the source change is committed and record that source revision.

AGENTS.md reference: AGENTS.md:L23-L27

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, and fixed in e690786. The source change is committed first (a5334a1), and the bundle was regenerated at that commit with python tests/test_precision.py 2>&1 | tee evidence/precision/precision_tests.log. The JSON records repo_commit a5334a1. The log and README now match: 21 test methods, 40,015 checks. Relative to main, the only changed count is crossover_implications (1536 to 1559), because the new test consumes the shared seeded stream; the README does not cite it.

Review of the previous commit: rounding each underflowing product before
the accumulation adds a rounding the modelled FMA chain does not perform.
For the leaves [2^-149, 2^-150] the FP32 sequential sum is a tie that
rounds to even (2^-148); rounding the second leaf first gave 2^-149.

Leaves below the normal range are now kept exact and only the modelled
accumulator operations round them; a chunk that is a single leaf is rounded
once at the end, an error already charged by error_bound's nodes(n) term.
The regression test includes the review's example.
@SamMausberg

Copy link
Copy Markdown
Owner Author

Verdict: REQUEST CHANGES

Reviewed head c7046f2 (6 behind main). The change removes the ValueError and keeps the error bound valid. However, both of Codex's P2 comments on this PR are correct, and under the standing rule both are blocking.

Blocking

B1. Pre-rounding subnormal leaves adds a rounding that the modelled kernels do not have (src/precision_reference.py:258-265, Codex P2).

  • What the models say. 'sequential' is documented as an FMA/add chain, and 'fused' as a multi-term adder over exact products. stock_logits (line 835) defines the stock head as "FP32 accumulation of exact BF16 products". In all three, a product enters the sum exactly and is rounded only together with the addition.
  • What the patch does. It rounds each subnormal product into FP32 before it is added. With ftz=False, FP32_SEQUENTIAL.sum([2^-149, 2^-150]) now returns 2^-149 (I reproduced this on c7046f2). An FMA chain gives 2^-148: 1.5 x 2^-149 rounds on the tie to the even 2^-148. The pre-rounding instead turns 2^-150 into 0 on its own tie.
  • Consequence. The enclosure remains sound, because error_bound still charges one underflow per leaf. But stock_logits and the rescoring rungs no longer compute the value the modelled kernel computes, and the reference exists to reproduce that value.
  • Fix.
    1. Keep leaves exact, as before, so the only rounding is at each add (_r(acc + t), fused_add), with ftz applied there.
    2. Handle the one case the old ValueError was protecting: a chunk or sum of a single leaf returning an unrounded value. Round the final result, self._r(acc), which is idempotent for representable values. Also round a single-leaf _pairwise result when it is a whole chunk.
    3. If 'pairwise' or 'blocked' are meant to model a separately rounded multiply, say so in their docstring and pre-round only there.
    4. Add Codex's example as a test: sequential FP32 without ftz on [2^-149, 2^-150] must give 2^-148, and with ftz must give 0.

B2. The regenerated evidence records a commit that does not contain the tested code (evidence/precision/precision_tests.json:5, Codex P2).

  • What is wrong. repo_commit is 25278b6, the parent commit, which has neither test_subnormal_products nor the new Accumulator.sum. precision_tests.log still shows the earlier 20-test run at 8296b585, and evidence/precision/README.md lines 11 and 18 still say 8296b585 and 20 test methods.
  • Consequence. The committed evidence contradicts itself about which code produced it.
  • Fix. After committing the B1 fix, rerun python tests/test_precision.py > evidence/precision/precision_tests.log (or however the log was made), so that repo_commit names a commit that contains the code. Update the README's commit, test count and check count to match.

Checked

  • Tests. python tests/test_precision.py passes all 21 methods on this head. The JSON counts reproduce exactly: 160 subnormal cases, 71 exact ties, crossover implications 1,559. Only repo_commit and seconds change.
  • The bound. underflow is min_normal with ftz and one subnormal spacing without it, which covers either rounding of a leaf. nodes(n) = 2n + splits charges one per leaf, so the bound itself is unchanged, as the PR says.
  • The new test. It covers a normal-range weight next to subnormal ones, both ftz settings and exact ties, and it still rejects an inexact normal-range leaf.
  • Hooks. Pre-commit passes on the changed source and test.
  • The 2^-100 GPU term. I will check the rescoring-radius term for the stock kernel's FP32 underflow in Certified int8 LM head: exact stock-head decisions, seeded sampling, kernels and evidence #45. 2K x 2^-126 <= 2^-100 needs K <= 2^25, which holds for K = 2,560.

@SamMausberg

Copy link
Copy Markdown
Owner Author

Verdict: APPROVE

This re-review covers head 751d78a. Both blocking items, which were Codex's two P2s, are fixed.

  • B1.
    • Underflowing leaves now stay exact: only an inexact normal-range leaf raises.
    • Each modelled operation rounds as before, and sum ends with self._r(acc). That covers a single-leaf chunk and a whole-chunk _pairwise leaf, is idempotent otherwise, and is charged by nodes(n) = 2n + splits.
    • The new assertions pin Codex's example: FP32_SEQUENTIAL.sum([2^-149, 2^-150]) == 2^-148; with ftz it is 0; and sum([2^-266]) in {0, 2^-149}.
  • B2.
    • precision_tests.json and precision_tests.log both record repo_commit 960aa80, which contains the source and test changes.
    • evidence/precision/README.md now states 960aa80, 21 test methods and 40,015 checks, and describes the subnormal cases.
    • python tests/test_precision.py on this head reproduces the JSON exactly, apart from repo_commit and seconds.
  • Hooks. Pre-commit passes on the changed source and test.

@SamMausberg
SamMausberg merged commit 52e984d into main Sep 30, 2026
SamMausberg added a commit that referenced this pull request Sep 30, 2026
…nel contract

The paper now argues in dependency order: setting and what the stock engine
computes, the five contracts and the R-stock gap and interval theorems, the
certified low-precision head with its measured candidate sets and fallback
shares, sampled decisions, transport and its measured failure, exactness under
speculation, the stack, drafting and repair hypotheses, the serving protocol,
related work, and a discussion that merges the hypothesis and failure tables.
Secondary derivations, untested mechanisms, engine notes, CPU evidence,
reproduction and attribution move to appendices. Evidence paths, pull request
numbers and pending work move to an evidence register and a development record
(Appendix A); the text cites [En] for committed evidence and [pending Dn] for
designed work, with the scientific status in the prose.

Integrates merged evidence: H2 on DFlash-4B (#35) and MTP-4B (#47), the
overlapped roofline (#44), the frontend diagnosis (#46), the fifth contract
witness (#41) and the precision counts after #43. Folds in the Codex items on
#21 (quantiles over all rows), #32 (pooled tau, GSM8K series) and #36.

Typography: Charter text, a plain title block with the repository URL,
greyscale-safe figures, a new mechanism figure (Figure 1) replacing three
overlapping flowcharts, a drift figure and transport table, two-panel
candidate and acceptance figures, and tables reorganized instead of shrunk.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant