Skip to content

perf(RingTheory/Kaehler/Polynomial): change universe name for speed-up - #34088

Closed
kbuzzard wants to merge 10 commits into
leanprover-community:masterfrom
kbuzzard:kbuzzard-alphabetical-universe
Closed

kbuzzard wants to merge 10 commits into
leanprover-community:masterfrom
kbuzzard:kbuzzard-alphabetical-universe

Conversation

@kbuzzard

@kbuzzard kbuzzard commented Jan 17, 2026 •

Copy link
Copy Markdown
Member

Changing the choice of R's universe to v from u in this file means that alphabetically it's after u_1 which is a workaround for the issue described here leanprover/lean4#12102 .

The bottom line is that Lean's defeq algorithm is now able to typecheck the left_inv and right_inv proofs in KaehlerDifferential.mvPolynomialEquiv over 200 times more quickly (for example right_inv goes from 54494621 heartbeats to 222096, i.e. from about 5 seconds to about 0.02 seconds on a typical machine).


Open in Gitpod

Before this PR, Lean was spending far too long in the proof fields of KaehlerDifferential.mvPolynomialEquiv, checking defeq on the following kinds of terms (with pp.all on):

[isDefEq] [815425.000000] ✅️ @Module.{max u u_1, max u_1 u} 
  (@MvPolynomial.{u_1, u} σ R (@CommRing.toCommSemiring.{u} R inst✝))
  (@KaehlerDifferential.{u, max u_1 u} R
[20 lines omitted]

=?= 

@Module.{max u u_1, max u u_1}
  (@MvPolynomial.{u_1, u} σ R (@CommRing.toCommSemiring.{u} R inst✝))
  (@KaehlerDifferential.{u, max u u_1} R
[pretty much the same 20 lines omitted except with max u_1 u replaced by max u u_1 sometimes]

with the crucial difference that the second universes in the terms are max u_1 u for the first and max u u_1 for the second. The proofs were taking a few seconds to compile, and then after compilation the kernel was also taking a few seconds to typecheck them. The alphabetical hack in this PR stops this happening and Lean's defeq algorithm now terminates essentially instantly on these terms.

Zulip thread: #mathlib4 > KaehlerDifferential.mvPolynomialEquiv typechecking @ 💬

@kbuzzard

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Jan 17, 2026 •

Copy link
Copy Markdown

Benchmark results for d7e3286 against 5c74f14 are in! @kbuzzard

Large changes (1✅)

  • ✅ build/module/Mathlib.RingTheory.Kaehler.Polynomial//instructions: -117.3G (-68.5%)

Small changes (1🟥)

  • 🟥 build/module/Mathlib.Tactic.ProdAssoc//instructions: +390.3M (+5.7%)

@github-actions github-actions Bot added the t-ring-theory Ring theory label Jan 17, 2026
@github-actions

github-actions Bot commented Jan 17, 2026 •

Copy link
Copy Markdown

PR summary b927f91230

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff

No declarations were harmed in the making of this PR! 🐙

You can run this locally as follows
## from your `mathlib4` directory:
git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci

## summary with just the declaration names:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh in the mathlib-ci repository contains some details about this script.


No changes to strong technical debt.
No changes to weak technical debt.

@erdOne

erdOne commented Jan 17, 2026

Copy link
Copy Markdown
Member

Is it the plan to get this merged or is this just to benchmark?
If it is the former please leave a comment similar to the one in Jacobi Zariski noting that there is something suspicious going on in the universes and referencing #26018.

@erdOne erdOne added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Jan 17, 2026
@mattrobball

Copy link
Copy Markdown
Contributor

My complaint is that we gain nothing beyond hacky changes doing things like. There is a pretty good chance there are improvements to level unification hiding in plain sight that would improve these two occurrences and also speed up things across the library.

When we do hacky fixes, we should ask why: is it because we need to cool math and this blocks it? If not, then we need either investigate more deeply or live with the friction until we can really address it. Papering over it is only going to hide other instances of the underlying issue until they drown us.

Where is the unification algorithm is the mismatched order triggering some branching?

@kbuzzard

kbuzzard commented Jan 18, 2026 •

Copy link
Copy Markdown
Member Author

Is it the plan to get this merged or is this just to benchmark?

I don't know. I'm going to re-open the universe discussion in #lean4 on the Zulip #lean4 > kernel universe normalization issue @ 💬

Edit: OK I've written everything I know in that thread.

My complaint is that we gain nothing beyond hacky changes doing things like [this]

We gain speed-up in building mathlib. I have documented the problem in #lean4 on the Zulip and this hacky fix will save us 117.3G instructions with every one of the hundreds of commits to this repo every day while people either discuss the problem or decide that it's a wontfix.

@kbuzzard

Copy link
Copy Markdown
Member Author

Where is the unification algorithm is the mismatched order triggering some branching?

See #lean4 > kernel universe normalization issue @ 💬 which indicates that the answer is "in at least two places", making me think that it's going to be difficult to "fix" the unification algorithm.

@kbuzzard

Copy link
Copy Markdown
Member Author

Is it the plan to get this merged or is this just to benchmark? If it is the former please leave a comment similar to the one in Jacobi Zariski noting that there is something suspicious going on in the universes and referencing #26018.

I've left a comment. I am hoping that I can ignite some discussion on Zulip about this; the last time this came up, the Lean devs found a fix which simply involved making a definition irreducible. My gut feeling is that more definitions in mathlib should be irreducible and if this file can be sped up with such ideas then that's great. However it's not immediately clear to me how to do this here. In the mean time it simply seems like a waste of CPU cycles to not merge this, but I'm not paying for these cycles so I really don't mind if it's merged or not. Ideally we will solve the underlying issue -- we could simply wait to see what happens or we could start tracking hacks like this as "technical debt" (even though "I am naming my universes in certain ways" doesn't really seem like debt).

@mattrobball

Copy link
Copy Markdown
Contributor

I'm confined to mobile during the weekend so rely on claude code from iOS. Turns out it can't push tags to get CI to run and generate a toolchain. But there is a branch on my fork with (supposedly) more logging with this issue.

If I had to guess there should be a cheap defeq check where syntactically same - minus levels -> norm levels and check again.

@kbuzzard kbuzzard added awaiting-zulip There is a Zulip discussion; the author should await and report/implement the decision reached there and removed awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. labels Jan 19, 2026
@kbuzzard

kbuzzard commented Jan 20, 2026 •

Copy link
Copy Markdown
Member Author

Mario has minimised the underlying universe issue #lean4 > kernel universe normalization issue @ 💬 so maybe there will be some further discussion there.

@kbuzzard kbuzzard removed the awaiting-zulip There is a Zulip discussion; the author should await and report/implement the decision reached there label Jan 24, 2026
@kbuzzard

kbuzzard commented Jan 24, 2026 •

Copy link
Copy Markdown
Member Author

There is now a Lean issue leanprover/lean4#12102 tracking the problem which the innocuous-looking change in this PR works around. I have added a comment referencing the issue.

@kbuzzard

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Jan 24, 2026 •

Copy link
Copy Markdown

Benchmark results for 52d8353 against 5c74f14 are in! @kbuzzard

  • ✅ build//instructions: -101.3G (-0.1%)

Large changes (1✅)

  • ✅ build/module/Mathlib.RingTheory.Kaehler.Polynomial//instructions: -94.8G (-55.3%)

Small changes (2✅, 1🟥)

  • ✅ build/module/Mathlib.Algebra.Ring.Rat//instructions: -464.7M (-6.8%)
  • 🟥 build/module/Mathlib.Tactic.ProdAssoc//instructions: +414.6M (+6.1%)
  • ✅ build/module/Mathlib.Util.ElabWithoutMVars//instructions: -264.6M (-9.9%)

@Vierkantor

Copy link
Copy Markdown
Contributor

I'm going to argue in favour of merging this PR. It is a hack that changing a variable name affects performance, sure, but it is also a clearly signposted example of a workaround for leanprover/lean4#12102. And having a fix + comment is going to be a lot more valuable to understanding the structure of the code and its effects than code we know to be slow but we don't do anything about. If the variables were "accidentally" named in alphabetic order the first time around, we wouldn't be having this discussion. So I don't see why it's worth having the same conversation now that we intentionally fix the issue. And there's no impact on the readability of this definition, nor do we get any noticeable slowdowns downstream.

In particular, I believe that there is a lot more motivation to fix leanprover/lean4#12102 if we have a lot of comments in Mathlib explicitly referring to workarounds for the bug, rather than a number of slow files in Mathlib and some unspoken community knowledge that some files could be slow due to leanprover/lean4#12102.

@kbuzzard kbuzzard changed the title fix: change universe name for speed-up perf: change universe name for speed-up Jan 27, 2026
@grunweg grunweg changed the title perf: change universe name for speed-up perf(RingTheory/Kaehler/Polynomial): change universe name for speed-up Jan 27, 2026
@j-loreaux

Copy link
Copy Markdown
Contributor

I agree with Anne's assessment here. I see no reason we shouldn't merge this even though it is a hack.

@kbuzzard

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Jan 31, 2026 •

Copy link
Copy Markdown

Benchmark results for a0a146b against 5c74f14 are in! @kbuzzard

  • ✅ build//instructions: -61.4G (-0.03%)

Large changes (1✅, 1🟥)

  • 🟥 build/module/Mathlib.RingTheory.Extension.Cotangent.Basic//instructions: +44.9G (+26.07%)
  • ✅ build/module/Mathlib.RingTheory.Kaehler.Polynomial//instructions: -127.7G (-74.53%)

Small changes (1✅, 2🟥)

  • 🟥 build/module/Mathlib.Algebra.Module.Presentation.Differentials//instructions: +1.2G (+3.51%)
  • ✅ build/module/Mathlib.Data.Nat.Notation//instructions: -111.4M (-4.72%)
  • 🟥 build/module/Mathlib.Tactic.ProdAssoc//instructions: +392.7M (+5.77%)

@kbuzzard

Copy link
Copy Markdown
Member Author

OK so Andrew's innocuous suggestion above ("remove (sigma : Type*) from a declaration and make it a variable, and then tidy up") manages to (a) make the changed file 30G instructions faster and (b) make another random file 45G instructions slower. I am happy to continue to investigate this; the PR should definitely not be merged at this point.

@kbuzzard

kbuzzard commented Feb 1, 2026

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Feb 1, 2026 •

Copy link
Copy Markdown

Benchmark results for 1e8f221 against 5c74f14 are in! @kbuzzard

  • 🟥 build//instructions: +1.7T (+0.86%)

Large changes (1✅, 5🟥)

  • 🟥 build/module/Mathlib.Algebra.Module.Presentation.Differentials//instructions: +334.0G (+955.88%)
  • 🟥 build/module/Mathlib.RingTheory.Extension.Cotangent.Free//instructions: +425.0G (+1520.79%)
  • 🟥 build/module/Mathlib.RingTheory.Kaehler.JacobiZariski//instructions: +634.5G (+270.79%)
  • ✅ build/module/Mathlib.RingTheory.Kaehler.Polynomial//instructions: -127.7G (-74.54%)
  • 🟥 build/module/Mathlib.RingTheory.Smooth.StandardSmoothCotangent//instructions: +367.6G (+1091.15%)
  • 🟥 build/profile/type checking//wall-clock: +2m 23s (+6.74%)

Medium changes (1🟥)

1 hidden

Small changes (2✅, 4🟥)

  • ✅ build/module/Mathlib.Analysis.Distribution.SchwartzSpace//instructions: -361.4M (-0.11%)
  • 🟥 build/module/Mathlib.RingTheory.Extension.Cotangent.Basic//instructions: +38.7G (+22.48%) (reduced significance based on *//lines)
  • 🟥 build/module/Mathlib.RingTheory.Smooth.StandardSmoothOfFree//instructions: +800.2M (+6.44%)
  • 🟥 build/module/Mathlib.Tactic.ProdAssoc//instructions: +397.3M (+5.84%)
  • ✅ build/module/Mathlib.Util.TransImports//instructions: -123.8M (-4.04%)
  • 🟥 build/profile/blocked (unaccounted)//wall-clock: +3m 15s (+2.05%)

@kbuzzard

kbuzzard commented Feb 1, 2026

Copy link
Copy Markdown
Member Author

Everything is so brittle!

@kbuzzard

kbuzzard commented Feb 2, 2026

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Feb 2, 2026 •

Copy link
Copy Markdown

Benchmark results for b0d14fe against 5c74f14 are in! @kbuzzard

  • ✅ build//instructions: -139.2G (-0.07%)

Large changes (1✅)

  • ✅ build/module/Mathlib.RingTheory.Kaehler.Polynomial//instructions: -94.7G (-55.29%)

Small changes (2✅, 18🟥)

  • 🟥 build/module/Aesop.ElabM//instructions: +287.0M (+10.63%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Aesop.Exception//instructions: +259.8M (+9.02%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Aesop.Frontend.Basic//instructions: +277.5M (+11.08%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Aesop.Index.DiscrTreeConfig//instructions: +282.8M (+11.47%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Aesop.Options.Public//instructions: +298.2M (+7.08%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Aesop.RuleSet.Name//instructions: +253.7M (+10.64%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Aesop.RuleTac.FVarIdSubst//instructions: +253.3M (+7.89%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Aesop.Script.GoalWithMVars//instructions: +270.1M (+10.49%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Aesop.Script.OptimizeSyntax//instructions: +252.7M (+6.49%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Aesop.Script.Tactic//instructions: +249.3M (+10.03%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Aesop.Util.Tactic.Unfold//instructions: +76.6M (+1.80%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Aesop.Util.Tactic//instructions: +96.6M (+3.73%)
  • 🟥 build/module/Aesop.Util.Unfold//instructions: +263.1M (+5.85%) (reduced significance based on absolute threshold)
  • ✅ build/module/Mathlib.Analysis.Distribution.SchwartzSpace//instructions: -526.4M (-0.16%)
  • 🟥 build/module/Mathlib.Tactic.ProdAssoc//instructions: +399.4M (+5.87%)
  • ✅ build/module/Mathlib.Tactic.SudoSetOption//instructions: -258.4M (-7.51%)
  • 🟥 build/module/Qq.ForLean.Do//instructions: +305.1M (+10.43%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Qq.ForLean.ReduceEval//instructions: +316.5M (+8.66%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Qq.ForLean.ToExpr//instructions: +269.7M (+8.81%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Qq.Typ//instructions: +297.2M (+10.38%)

@kbuzzard

kbuzzard commented Feb 3, 2026

Copy link
Copy Markdown
Member Author

!radar

@leanprover-radar

leanprover-radar commented Feb 3, 2026 •

Copy link
Copy Markdown

Benchmark results for 992b41c against 33a2f73 are in! @kbuzzard

  • ✅ build//instructions: -115.4G (-0.06%)

Large changes (1✅, 1🟥)

  • ✅ build/module/Mathlib.RingTheory.Kaehler.Polynomial//instructions: -90.1G (-58.19%)
  • 🟥 build/profile/grind dsimp//wall-clock: +14ms (+459.41%)

@kbuzzard kbuzzard removed the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Feb 3, 2026
@kbuzzard

kbuzzard commented Feb 3, 2026 •

Copy link
Copy Markdown
Member Author

Ok I don't know how to make this file any better without making other files worse, so I'm going to stop fiddling. The grind dsimp thing is just noise -- it's been up and down loads of times in the last few days on random PRs.

@Vierkantor Vierkantor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the experiments! I'll merge this PR in 24 hours, unless someone objects.

@erdOne

erdOne commented Feb 4, 2026

Copy link
Copy Markdown
Member

I am less sure if we should merge it now that Kevin's new experiments seem to suggest that we have yet to fully grasp what's going on.

@kbuzzard

kbuzzard commented Feb 5, 2026 •

Copy link
Copy Markdown
Member Author

I think I grasp what is going on. I think we've established that if Lean has to unify foo.{max u v} with foo.{max v u} for an expensive-to-unfold foo, then this hurts. This is going on all the time in certain files. One can attempt to fix this by making a random local change e.g. an alphabetical universe change; this then changes max u v to max v u for some random collection of declarations and theorems, or it might change bar.{u, v} to bar.{v, u}. So it might then happen that some things that used to be quick get slow, and some things that used to be slow get quick, and the result is random.

A concrete example in the file I'm editing here is def KaehlerDifferential.mvPolynomialBasis. This is unchanged by this PR. Right now it looks like def KaehlerDifferential.mvPolynomialBasis (σ) which means that a universe name is only assigned for σ's universe late on, which mean that when some universe-ordering is taking place we have max v ? which is left alone, and then later on σ is assigned the universe u_1 so we end up with a max v u_1. However if we actually name the universe which σ is living in, then we can either choose a letter before or after v and this gives two new behaviours. You can see the three behaviours here #lean4 > kernel universe normalization issue @ 💬 . In all cases all that is happening is that some changes happen, so some slow things get fast and some fast things get slow. I did not indicate in that link what happens to other files, but basically random things happen.

The nice thing about this PR is that some slow things get fast, but no fast things get slow.

It might be possible to continue with hacks like this, with each move making some things fast and no things slow. I don't intend to do this.

I think it's also worth talking about why I made this PR at all. In #34077 I make a priority change which I think is compatible with how mathlib thinks about these priorities. Build instructions go down by 283G. However Mathlib.RingTheory.Kaehler.Polynomial goes up by 85G. I conjecture (but never actually checked) that this PR will stop that +85G from happening. In fact I will check this now.

@kbuzzard

kbuzzard commented Feb 5, 2026 •

Copy link
Copy Markdown
Member Author

Yeah, as I suspected, #34077 (which is not a hack but warrants discussion) plus the hack here saves 463G.

@kbuzzard

kbuzzard commented Feb 7, 2026 •

Copy link
Copy Markdown
Member Author

I am less sure if we should merge it now that Kevin's new experiments seem to suggest that we have yet to fully grasp what's going on.

Note that leanprover/lean4#12102 is currently labelled P-high so one possibility is that we wait and see what happens there. Edit: I've marked this PR as WIP for now. I think that if core are going to move then there is little point in merging this.

@kbuzzard

kbuzzard commented Feb 7, 2026

Copy link
Copy Markdown
Member Author

NB I am not going to do any more experiments on this branch now; there is a lot of benchmarking but the relevant one is the last one, with

✅ build/module/Mathlib.RingTheory.Kaehler.Polynomial//instructions: -90.1G (-58.19%)

This is caused by a universe alphabetical reordering giving a huge speed-up of the declaration KaehlerDifferential.mvPolynomialEquiv. There are still more savings which one can make in this file (KaehlerDifferential.mvPolynomialBasis can also be sped up, giving a total saving in this file of -127.7G; see for example #34088 (comment) above; the problem with this change is that it makes other files slower).

The reason I mention all of this is just to note that the target for a core fix in this file Mathlib.RingTheory.Kaehler.Polynomial would be -127.7G or more, as that is a better measure of the total gains in this particular file which can be currently achieved by universe hacks. This PR does not make all of these hacks as they cause problems elsewhere.

@kbuzzard kbuzzard mentioned this pull request Feb 7, 2026
@kbuzzard kbuzzard added the WIP Work in progress label Feb 7, 2026
@kbuzzard

Copy link
Copy Markdown
Member Author

Just to check if this issue is still present.

!radar

@leanprover-radar

leanprover-radar commented May 15, 2026 •

Copy link
Copy Markdown

Benchmark results for b927f91 against 54f98fd are in. No significant results found. @kbuzzard

  • ✅ build//instructions: -35.0G (-0.02%)

Small changes (2✅)

  • ✅ build/module/Mathlib.CategoryTheory.Limits.Shapes.SingleObj//instructions: -376.1M (-5.38%)
  • ✅ build/module/Mathlib.RingTheory.Kaehler.Polynomial//instructions: -3.8G (-9.99%)

@kbuzzard

Copy link
Copy Markdown
Member Author

It's not!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t-ring-theory Ring theory WIP Work in progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants