Repository navigation
perf(univariate): compute each entry of a raw product as one sum - #422
Conversation
🤖 PR SummaryPerformance: compute each entry of a raw product as one sumThis PR introduces Mathematical Formalization
Proof Completion (removed sorries/admits)
Code Organization and Refactoring
Infrastructure / CI
Performance (Benchmark Results)
No Statistics
Lean Declarations ✏️ Removed: 6 declaration(s)
✏️ Added: 30 declaration(s)
📋 **Additional Analysis**FindingsStyle and Naming Guideline Violations
Performance Contribution Rule Checks
Documentation Consistency
Other Observations
SummaryThe most critical issues are the invalid syntax in the test file ( 📄 **Per-File Summaries**
Last updated: 2026-11-20 02:07 UTC. |
Build Timing Report
Incremental Rebuild Signal
Slowest Current Build Files (warm library build)Showing 20 slowest current targets from the warm library build log, with comparison against the baseline warm library build log when available.
|
04fff5a to
27841e8
Compare
3ec583e to
9a9d74d
Compare
…cement The refinement proof of mulRaw_eq_mulRawFast now closes with one simp only over named lemmas: Trim.coeff_eq_getElem, coeff_mulRaw and getElem_mulRawFast. The proof no longer depends on coeff unfolding to getD. The private natFold_push is now the public Nat.fold_push_eq_append_ofFn in CompPoly/Data/Array/Lemmas.lean. It is a fact about arrays only. natFold_add_eq_sum stays private, because that module has no big-operator import. The test module gains new checks. ZMod 6 and 2 x 2 matrices over ZMod 4 test coefficient types with zero divisors. An IR check reads the compiled code of mul and fails if it does not call mulRawFast. The test prose qualifies the output length with "for a nonempty p", and the test functions use the arrow ↦. The other changes are text only. The header of Raw/Ops.lean has one copyright line, which keeps the ArkLib attribution. The module docstring names the size lemmas and the csimp lemma. The csimp docstring states the claim of the lemma. The repo map says where the size equations are. The autoresearch row cites #422 and states the operation count of the loop. Compatibility: add_coeff? and smul_mulPowX_coeff keep their names and namespaces. The first commit of this PR changed their instance arguments. The lemma add_coeff? takes [AddZeroClass Q] in place of [Semiring Q]. The lemma smul_mulPowX_coeff takes [MulZeroClass R] in place of [Semiring R] [BEq R] [LawfulBEq R]. The new statements are more general. A caller that passes these instances explicitly with @ can need an edit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9a9d74d to
d3b1a9c
Compare
|
This PR is now rebased onto the fix-up of #421 (
The fix-up changes no definition under Validation at
Corrections to the descriptionTiming claims. The pinned run (5 rounds per side,
The 14
The rows that use NTT multiplication or NTT interpolation serve as controls. There are 19 of them, not 16: 16 NTT multiplication rows and 3 NTT interpolation rows. All 19 are Measured commits. The two binaries of this run were built at
In those binaries the Mutation check. The run had 12 mutants: 11 edits to Other corrections.
|
Raw.mulRaw folds over the coefficients of p and adds the shifted row p[i] * q to the accumulator. Row i has q.size + i entries, so a nonempty p costs p.size * q.size + p.size * (p.size - 1) / 2 additions. A short right factor, as in p * C c or p * (X - C a), costs a number of additions that is quadratic in p.size (#419). The new Raw.mulRawFast builds the result in one pass. Entry k is the sum of p[i] * q[k - i] over the indices i that land in it, so the loop does p.size * q.size multiplications and as many additions. It reads the coefficients with proved indices. The raw array is the array of mulRaw: an empty p gives an empty array, and an empty q gives p.size - 1 zeros. Each product keeps the order p[i] * q[j], so no commutativity is needed. The loop takes 0, + and * as projections of the Semiring structure. The default instance paths construct Distrib and MulZeroClass values, and code specialized at ZMod constructed them in every iteration. The @[csimp] lemma mulRaw_eq_mulRawFast makes compiled code use the new loop. The lemma is in Raw/Ops.lean before mul, because mul and powBySqUntrimmed inline or specialize mulRaw when they are compiled. mulRaw stays the definition. The proof computes each entry of both arrays as the same convolution sum. To give the proof the lemmas it needs, size_smul, size_mulPowX, size_addRaw and size_mulRaw move from Raw/Size.lean to Raw/Ops.lean, and add_coeff? and smul_mulPowX_coeff move from Raw/Proofs.lean to Raw/Ops.lean. Their names do not change. add_coeff? now needs only AddZeroClass and smul_mulPowX_coeff only MulZeroClass. The new test module compares the compiled code with a copy of the definition of mulRaw. The inputs include empty and trailing-zero arrays, short factors on each side, and 2 x 2 matrices over Nat, which do not commute. Closes #419 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cement The refinement proof of mulRaw_eq_mulRawFast now closes with one simp only over named lemmas: Trim.coeff_eq_getElem, coeff_mulRaw and getElem_mulRawFast. The proof no longer depends on coeff unfolding to getD. The private natFold_push is now the public Nat.fold_push_eq_append_ofFn in CompPoly/Data/Array/Lemmas.lean. It is a fact about arrays only. natFold_add_eq_sum stays private, because that module has no big-operator import. The test module gains new checks. ZMod 6 and 2 x 2 matrices over ZMod 4 test coefficient types with zero divisors. An IR check reads the compiled code of mul and fails if it does not call mulRawFast. The test prose qualifies the output length with "for a nonempty p", and the test functions use the arrow ↦. The other changes are text only. The header of Raw/Ops.lean has one copyright line, which keeps the ArkLib attribution. The module docstring names the size lemmas and the csimp lemma. The csimp docstring states the claim of the lemma. The repo map says where the size equations are. The autoresearch row cites #422 and states the operation count of the loop. Compatibility: add_coeff? and smul_mulPowX_coeff keep their names and namespaces. The first commit of this PR changed their instance arguments. The lemma add_coeff? takes [AddZeroClass Q] in place of [Semiring Q]. The lemma smul_mulPowX_coeff takes [MulZeroClass R] in place of [Semiring R] [BEq R] [LawfulBEq R]. The new statements are more general. A caller that passes these instances explicitly with @ can need an edit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… code The placement test now walks the compiled code transitively from test definitions that use mul, pow and powBySq. Each must reach code of mulRawFast. Closed terms do not count, because the compiler shares them between unrelated declarations. A new test fails if the code of mulRaw specialized at ZMod 6 calls instDistribOfSemiring or instMulZeroClassOfSemiring. The comment at the letI lines of mulRawFast names this test. New checks compare mulRaw with its defining fold at 70 x 33 and at 300 x 2. The four products with an empty factor that RawSize.lean already checks are removed from RawMul.lean. Nat.fold_push_eq_append_ofFn now lets the pushed element depend on the bound proof. The private sum_range_eq_sum_Ico is now sum_range_ite_eq_sum_Ico. The docstrings of Raw/Ops.lean and Raw/Size.lean name the lemmas that each file states. The provenance of the ported size lemmas moves to the module docstring of Raw/Ops.lean. The size_mulRawFast and powBySqUntrimmed docstrings, the test docstrings, the repo map and the autoresearch row are corrected. The autoresearch row records the 0 + x addition in each entry as a next step. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d3b1a9c to
0bdde4b
Compare
Raw.mulRaw p qfolds over the coefficients ofpand adds the shifted rowp[i] * qto the accumulator. Rowihasq.size + ientries, so a nonemptypcostsp.size * q.size + p.size * (p.size - 1) / 2additions. Whenqis short, as inp * C corp * (X - C a), the second term dominates and the cost is quadratic inp.size(#419). This PR addsRaw.mulRawFast, which builds the result in one pass and computes entrykas one sum, and connects it tomulRawwith a@[csimp]lemma.This PR is stacked on #421, which adds the groups
univariate-raw-mul-short-*andunivariate-raw-mul-balanced-*. CONTRIBUTING asks for a new benchmark group in its own PR. Review the bench PR first.Change
Raw.mulRawFast(CompPoly/Univariate/Raw/Ops.lean). Entrykis the sum ofp[i] * q[k - i]overk + 1 - q.size ≤ i < min (k + 1) p.size, added to0in increasing order ofi. The loop readspandqwith proved indices, so it does no bounds checks. Each pair(i, j)withi < p.sizeandj < q.sizelands in exactly one entry, so the loop doesp.size * q.sizemultiplications and the same number of additions. The docstring states this count from the structure of the loop. It is not a proved theorem.0,+and*as projections of theSemiringstructure. Through the default instances, code specialized atBN254.ScalarField(ZMod) calledinstDistribOfSemiringin every iteration of the inner loop andinstMulZeroClassOfSemiringin every iteration of the outer loop. With the projections, the specialized IR has no such call. AtKoalaBear.Fast.Fieldthe specialized IR keeps unboxedu32arithmetic.Raw.mulRawdoes quadratic additions when the right factor is short #419). An emptypgives#[], and an emptyqgivesp.size - 1zeros, sosize_mulRawand the other raw equations still hold as stated. Each product keeps the orderp[i] * q[j], so no commutativity is assumed.@[csimp] theorem mulRaw_eq_mulRawFast : @mulRaw = @mulRawFast.mulRawstays the definition. The docstrings ofmulRawandmulsay that compiled code runsmulRawFast.Raw/Ops.leandirectly aftermulRawand beforemul. A@[csimp]lemma does not change code that the compiler already compiled.mul,powandpowBySqUntrimmedinline or specializemulRawwhen they are compiled, so a lemma in a later module would not reach them.docs/wiki/repo-map.mdsays where the size equations now live.Proof
The refinement theorem closes over the lemma sets of the two sides: both arrays have the same size, and each entry equals the convolution sum
∑ i ∈ range p.size, if k < i then 0 else p.coeff i * q.coeff (k - i).size_mulRawFastandgetElem_mulRawFast(both@[simp]), from the privatenatFold_push,natFold_add_eq_sumandsum_range_eq_sum_Ico.size_mulRaw, andcoeff_mulRaw(new, public, needs only[Semiring R]), from a fold induction over the rows ofmulRawthat usesadd_coeff?andsmul_mulPowX_coeff.The proof needs these existing lemmas in
Raw/Ops.lean, so they move there. Their names and namespaces do not change, so no caller changes and no deprecated alias is needed:size_smul,size_mulPowX,size_addRaw,size_mulRawRaw/Size.leanadd_coeff?Raw/Proofs.lean[Semiring Q]generalized to[AddZeroClass Q]smul_mulPowX_coeffRaw/Proofs.lean[Semiring R] [BEq R] [LawfulBEq R]generalized to[MulZeroClass R]Raw/Size.leanimportsRaw/Ops.leanpublicly and keepssize_C,size_mulRaw_leand the deprecatedadd_size. The note that the size facts were ported from ArkLib moves with them. All downstream proofs compile unchanged.axiomsweepreports nosorryAx. The axioms ofmulRaw_eq_mulRawFastarepropext,Classical.choiceandQuot.sound.The
natFold_*helpers stay private. No module belowRaw/Ops.leanfits them:CompPoly/Data/Array/Lemmas.leanhas no big-operator import, and the repository has several similar private fold-sum lemmas (for example inMultivariate/HornerLemmas.leanandLinearAlgebra/PolynomialMatrix/StrassenCorrectness.lean). One shared module for all of them is a separate change.Tests
tests/CompPolyTests/Univariate/RawMul.lean(new).mulRawRefrepeats the defining fold ofmulRaw(example : mulRawRef p q = mulRaw p q := rfl), and no@[csimp]lemma applies to it.#guardcompiles its term and runs the compiler's output in the interpreter, so the checks runmulRawFastthrough the@[csimp]lemma. They cover:#[c],#[-a, 1]) and a short left factor overℤ;ℕ, sizes 40 by 1 to 3 in both orders, and 24 by 24;2 × 2matrices overℕ, which do not commute: hand-computed products that fix the order of the factors, and all pairs of sizes 0 to 6 againstmulRawRef;mulRawFastcalled directly againstmulRawRef;run_metacheck that the@[csimp]map sendsmulRawtomulRawFast.These checks do not run native code.
CompPolyNativeSmokepasses, but it checks field arithmetic and does not target raw polynomial multiplication.Mutation check: with the refinement proof replaced by
sorry, 13 edits tomulRawFastwere tried. Three no longer compile, because a proved index bound fails (hi := min (k + 2) …,lo := k - q.size,q[k - (lo + t) + 1]). The test module catches the factor swap,lo := k + 2 - q.size, the empty-qand empty-pchanges, the lengths- 2and+ 0, dropping the first term, and removing@[csimp].p * q + sin place ofs + p * qsurvives; it is equivalent, because addition is commutative. The unmutated file withsorrypasses.Measurement
./scripts/bench-ab.sh run <groups>used themediumpreset and five rounds per side. Both binaries ran on two hardware threads of one performance core (taskset -c 2,3). The baseline was built at32296bdwith the old fold, and the candidate atb435dc4. The benchmark commit04fff5adiffers from that baseline only in benchmark documentation. The final fix3ec583ediffers from the measured candidate only in attribution and documentation.univariate-raw-mul-*groups,univariate-mul-crossover-{16,64,256,1024},univariate-mul-{koalabear,babybear}andunivariate-interp-koalabear-l6.same, 29faster(14 flaggedSUSPECT), and 0slower. There were no mismatches, missing rows, or unjudged rows.The machine was shared with other Lean builds. Several unpinned runs had drift outside ±10%, and the run above has per-row spreads up to about ±30%. None of the runs had a
slowerrow for this code.The 16 NTT rows do not call
mulRaw, so they serve as controls. They are omitted below. In this run all aresame, with ratios 1.03 to 1.23. In the unpinned runs they ranged from 0.67 to 1.39. For the short-left and balanced rows classifiedsame, the comparison detected neither a speedup nor a slowdown.The 14
SUSPECTflags all come from the ratio falling below 0.1 on rows with the short factor on the right. Removing the quadratic term gives a reason to expect a large speedup. These checks support that explanation, with the flagged ratios still subject to measurement noise.mismatch, so both binaries computed the same digests. Each right-factor row also shares a digest class with the left-factor row of the same product.Int. In the baseline the same ratios are 116 to 265. The left-factor rows are notSUSPECT.scalar-rightgrows ×5.9 from size 8 to 64 and ×14.6 from 64 to 1024. The baseline grows ×15.8 and ×88.0.mulRawFastat each of the three coefficient types has the multiplication and the addition inside the inner loop.BN254 with a short left factor
An earlier version of the loop used the default instance paths. At
BN254.ScalarFieldwith a1 × 1024factor pair it was 1.14 to 1.29 times slower than the old fold. The specialized code builtDistribandMulZeroClassvalues in every iteration. Hoisting0,+and*, or the instances, intoletbindings did not help: the specializer copies a let binding that depends only on the instance back into the loop. The structure projections remove these calls from the IR. In the run above the BN254 left-factor rows have ratios 0.63 to 0.70 (same, because the runs did not separate) and the balanced BN254 rows 0.65.Alternatives measured
getDreads in place of proved indices. Against it, in twobench-ab.shruns with drift up to 1.26 (outside ±10%), the proved-index loop had 0slowerrows. Its ratios were 0.41 to 0.65 on the KoalaBearmulRawrows, 0.57 to 0.99 on the KoalaBearCPolynomialrows, and 0.90 to 1.15 (same) on the BN254,Intand balanced rows. Against the old fold, thegetDloop readsloweron the KoalaBear1 × 1024left rows (2.11 and 1.34) in a run with drift inside ±10%. The proved-index loop readssameon those rows.getD, two bench binaries compiled from the samemulRawFastsource differed by a factor of about 2.2 on the KoalaBear1024 × 1and1 × 1024rows. The generated C of the loop was the same in both, so the difference comes from native code generation or layout. Read ratios on these rows across binaries with care.same, because the runs did not separate.KoalaBear.Fast.Field(ratio 1.08 to 1.22).Array.ofFnwas not specialized. It was slower forC con the left of a factor of size 1024 (ratio 1.92 to 1.95).Validation
All commands ran on this commit:
lake build --wfail: passes, no warnings.lake testandlake build --wfail CompPolyTests: pass, no warnings.lake exe axiomsweep --check: passes, 0sorryAx, no change toscripts/axiom_baseline.json../scripts/lint-style.sh,./scripts/check-imports.sh: pass.lake build CompPolyBenchLib CompPolyBenchandlake exe CompPolyBench --medium --validate-only: pass (500 records in 135 groups).CompPolyNativeSmokewith the limits intests/README.md: passes.scripts/check-docs-integrity.py: passes.scripts/check-writing.py: advisory sentence-length notes only.Not run: the CI clean rebuild and the benchmarks on the CI runner.
Follow-up, not part of this PR:
coeff_mulRawhas the same right-hand side asmul_coeff_range_sizeinRaw/Proofs.leanand could replace thecoeff_mulchain behind it.Closes #419
🤖 Generated with Claude Code