Skip to content

perf(RingTheory/Kaehler/JacobiZariski): reorder universe variables#21129

Closed
chrisflav wants to merge 4 commits intomasterfrom
jacobi-zariski-universe
Closed

perf(RingTheory/Kaehler/JacobiZariski): reorder universe variables#21129
chrisflav wants to merge 4 commits intomasterfrom
jacobi-zariski-universe

Conversation

@chrisflav
Copy link
Copy Markdown
Member

@chrisflav chrisflav commented Jan 27, 2025

Naming of universe variables has an impact on universe unification. It seems to matter that the universe variables of R, S and T appear before the universe variables of the generators in alphabetical order.


Open in Gitpod

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 27, 2025

PR summary 4466755c92

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
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@github-actions github-actions bot added the t-algebra Algebra (groups, rings, fields, etc) label Jan 27, 2025
@chrisflav
Copy link
Copy Markdown
Member Author

!bench

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit dbb8c6d.
There were significant changes against commit efeadfb:

  Benchmark                                   Metric         Change
  =================================================================
+ ~Mathlib.RingTheory.Kaehler.JacobiZariski   instructions   -29.0%

@github-actions
Copy link
Copy Markdown

File Instructions %
build -225.54⬝10⁹ (-0.14%)
Mathlib.RingTheory.Kaehler.JacobiZariski -210.979⬝10⁹ (-29.00%)
CI run

@chrisflav chrisflav changed the title test: reorder universe variables in jacobi zariski file perf(RingTheory/Generators): reorder universe variables in jacobi zariski file Jan 27, 2025
@chrisflav chrisflav marked this pull request as ready for review January 27, 2025 15:36
@chrisflav chrisflav added the performance-hack Something that improves performance but is poorly understood. label Jan 27, 2025
@grunweg grunweg changed the title perf(RingTheory/Generators): reorder universe variables in jacobi zariski file perf(RingTheory/Kaehler/JacobiZariski): reorder universe variables Jan 27, 2025
Copy link
Copy Markdown
Contributor

@grunweg grunweg left a comment

Choose a reason for hiding this comment

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

Wow, these are great speed-ups! These should definitely land soon.

Do you understand why this changes performance? If so, it would be good to document (and look for other instances in mathlib/perhaps even write a linter). If not, can you start a discussion on zulip?

I'll be bold nominate for a quick merge, mostly to get the ball rolling.
maintainer delegate?

@github-actions
Copy link
Copy Markdown

🚀 Pull request has been placed on the maintainer queue by grunweg.

@github-actions github-actions bot added the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Jan 27, 2025
@chrisflav
Copy link
Copy Markdown
Member Author

Wow, these are great speed-ups! These should definitely land soon.

Do you understand why this changes performance? If so, it would be good to document (and look for other instances in mathlib/perhaps even write a linter). If not, can you start a discussion on zulip?

I'll be bold nominate for a quick merge, mostly to get the ball rolling. maintainer delegate?

You probably missed the discussion at https://leanprover.zulipchat.com/#narrow/channel/144837-PR-reviews/topic/.2321099.20use.20unification.20hints.20for.20generators.

So @mattrobball and @erdOne should have a look first.

@mattrobball
Copy link
Copy Markdown
Contributor

I think changing the convention for naming universes in this file is not a good idea. I am pretty sure that the kernel is able to check equality of levels from the names directly and that is what allows it avoid many unfolding steps. I am less sure about the importance of the ordering.

@chrisflav
Copy link
Copy Markdown
Member Author

I think changing the convention for naming universes in this file is not a good idea. I am pretty sure that the kernel is able to check equality of levels from the names directly and that is what allows it avoid many unfolding steps. I am less sure about the importance of the ordering.

It definitely matters that the universes of R, S and T appear before the universes of P, Q, etc. alphabetically, just replace a, b and c by x, y and z respectively and many declarations even start to timeout with maxHeartbeats reached.

@mattrobball
Copy link
Copy Markdown
Contributor

I should be clear since there are two possible orderings: 1. declaration ordering (ie what line number/column does a level appear) and 2. orderings on the names via the usual lexicographical ordering on strings.

The former doesn't seem to matter. The latter seems to matter both for the elaborator and the kernel. Lexical ordering gives equality in the kernel.

@chrisflav
Copy link
Copy Markdown
Member Author

I should be clear since there are two possible orderings: 1. declaration ordering (ie what line number/column does a level appear) and 2. orderings on the names via the usual lexicographical ordering on strings.

The former doesn't seem to matter. The latter seems to matter both for the elaborator and the kernel. Lexical ordering gives equality in the kernel.

So what exactly do you suggest to change in this PR?

@erdOne
Copy link
Copy Markdown
Member

erdOne commented Jan 27, 2025

I think it is possible to preserve the alphabetical order while having universe names adhering to the naming convention.
And I'm guessing that is what Matthew is also suggesting?

Copy link
Copy Markdown
Contributor

@mattrobball mattrobball left a comment

Choose a reason for hiding this comment

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

These seem to give the same benefit and more closely follow the naming conventions in place.

This is very brittle though.

@chrisflav
Copy link
Copy Markdown
Member Author

Aha, thanks for the clarification, changed it accordingly.

@chrisflav
Copy link
Copy Markdown
Member Author

!bench

Copy link
Copy Markdown
Contributor

@mattrobball mattrobball left a comment

Choose a reason for hiding this comment

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

These would be better and still seem to perform the same. Generally, the user should not have to guess at the alphabetical orderings of names to get reasonable performance in elaboration and type checking.

namespace Algebra

universe u₁ u₂ u₃ u₄ w' w u v uT
universe w₁ w₂ w₃ w₄ w₅ u₁ u₂ u₃
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.

Suggested change
universe w₁ w₂ w₃ w₄ w₅ u₁ u₂ u₃
universe w₁ w₂ w₃ w₄ w₅

Comment on lines +36 to +37
variable {R : Type u₁} {S : Type u₂} [CommRing R] [CommRing S] [Algebra R S]
variable {T : Type u₃} [CommRing T] [Algebra R T] [Algebra S T] [IsScalarTower R S T]
Copy link
Copy Markdown
Contributor

@mattrobball mattrobball Jan 27, 2025

Choose a reason for hiding this comment

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

Suggested change
variable {R : Type u₁} {S : Type u₂} [CommRing R] [CommRing S] [Algebra R S]
variable {T : Type u₃} [CommRing T] [Algebra R T] [Algebra S T] [IsScalarTower R S T]
variable {R S T : Type*} [CommRing R] [CommRing S] [CommRing T]
variable [Algebra R S] [Algebra R T] [Algebra S T] [IsScalarTower R S T]

Copy link
Copy Markdown
Member Author

@chrisflav chrisflav Jan 27, 2025

Choose a reason for hiding this comment

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

Isn't this just working because the automatically introduced universe variables u_i are still alphabetically at the right place? So it seems this is even harder to control.

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.

Yes they are generated as u_i and we still need u < w for this hack. I don't see an advantage to the current situation and this is easier to digest for a reader.

Before merging, this PR will need some documentation with a link to an issue somewhere tracking this so readers won't have to guess at why things are the way they are.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I personally think if we are intentionally controlling the names of universes we should be explicit about them.

end Generators

variable {T : Type w} [CommRing T] [Algebra R T] [Algebra S T] [IsScalarTower R S T]
variable {T : Type u₃} [CommRing T] [Algebra R T] [Algebra S T] [IsScalarTower R S T]
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.

Suggested change
variable {T : Type u₃} [CommRing T] [Algebra R T] [Algebra S T] [IsScalarTower R S T]
variable {T : Type*} [CommRing T] [Algebra R T] [Algebra S T] [IsScalarTower R S T]

@mattrobball
Copy link
Copy Markdown
Contributor

Is there an open issue on anything like this with core?

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit 8700e71.
There were significant changes against commit efeadfb:

  Benchmark                                   Metric         Change
  =================================================================
+ ~Mathlib.RingTheory.Kaehler.JacobiZariski   instructions   -29.1%

@github-actions
Copy link
Copy Markdown

File Instructions %
build -213.23⬝10⁹ (-0.14%)
Mathlib.RingTheory.Kaehler.JacobiZariski -211.705⬝10⁹ (-29.10%)
CI run

@mattrobball
Copy link
Copy Markdown
Contributor

I think I need to take a little time to evaluate this holistically. I am concerned we are papering over problems which will only become worse with time.

@kbuzzard
Copy link
Copy Markdown
Member

kbuzzard commented Jan 28, 2025

I raise your 29% to 60% ;-)

#21165 (comment)

@chrisflav
Copy link
Copy Markdown
Member Author

chrisflav commented Jan 28, 2025

I raise your 29% to 60% ;-)

You did not follow the unwritten rules of this competition which clearly state without changing the results of this file. :)

@kbuzzard
Copy link
Copy Markdown
Member

But I saved half a trillion instructions! That is a huge number!

I've closed the PR in case anyone thought it was serious.

How do we move forwards on this? Presumably someone needs to find some kind of example, ideally mathlib-free, showing where the problem is? This might be really hard.

@mattrobball
Copy link
Copy Markdown
Contributor

mattrobball commented Jan 28, 2025

I am not sure that this PR is more serious than #21165. The latter does follow existing patterns in Mathlib.

Given @kbuzzard 's comment I am becoming more skeptical that the current design is aligned with how Lean works, currently.

  1. We should raise the issue with core to get their opinion on design. Note that δAux_toAlgHom seems a prime example for this comment
  2. We can attempt a fix to the elaborator and/or kernel to see if indeed inconsistent universe normalization of nested max's is a/the culprit here.
  3. We can, and probably should, attempt to unbundle the type fields from the offending structures.

@sgouezel sgouezel removed the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Feb 5, 2025
@kbuzzard
Copy link
Copy Markdown
Member

kbuzzard commented Mar 10, 2025

This has now come up in the triage meeting because it's one of the longest open PRs on the queue. One way of proceeding is that we document that this is a hack and merge it because at the end of the day it's making mathlib compile more quickly for everyone. What do people think about this?

PS there doesn't seem to be a link to the relevant Zulip thread, which is here.

@mattrobball
Copy link
Copy Markdown
Contributor

I still worry we are just digging ourselves deeper into a hole with the current designs.

@leanprover-community-bot-assistant leanprover-community-bot-assistant added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jun 2, 2025
@leanprover-community-bot-assistant
Copy link
Copy Markdown
Collaborator

This pull request has conflicts, please merge master and resolve them.

@github-actions github-actions bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jun 17, 2025
@chrisflav
Copy link
Copy Markdown
Member Author

This PR has been migrated to a fork-based workflow: #26008

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

Labels

migrated-to-fork performance-hack Something that improves performance but is poorly understood. t-algebra Algebra (groups, rings, fields, etc)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants