-
Notifications
You must be signed in to change notification settings - Fork 367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(Data/Nat): faster computation of Nat.log #17325
base: master
Are you sure you want to change the base?
Conversation
PR summary 5e2f2d7089Import changes for modified filesNo significant changes to the import graph Import changes for all files
|
#eval Nat.logTR 2 (2 ^ 1000000) | ||
#eval Nat.logC 2 (2 ^ 1000000) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kim-em, what's the SOTA for committing benchmarks / performance tests like these? It would be great to have a test file that computes a big logarithm, and even better if we can assert it is "quick".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that I read it again, the point of this was that Nat.logTR
times out on this computation whereas Nat.logC
doesn't, and Nat.log
hits stack limit at something like Nat.log 2 (2 ^ 11000)
.
I wouldn't mind removing these lines either, it's largely to save a future person's time if they think "I wonder if tail-recursion would be better than this weird doubling algorithm"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My comment really was "let's add a test somewhere of something that didn't work before your change"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm inclined to just create tests/nat/log.lean
, if these are meant to be regression tests.
If they are documentation, they should be code blocks inside a module doc next to the definition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong preference here. What's the distinction between having a code block as a module doc as opposed to a code block in the definition docstring?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean in particular that these aren't meant as regression tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm inclined to just create
tests/nat/log.lean
, if these are meant to be regression tests.
We should add this file, with an invocation that took >20s before and now takes <1s.
Co-authored-by: Yury G. Kudryashov <[email protected]>
Adapted from https://downloads.haskell.org/~ghc/9.0.1/docs/html/libraries/ghc-bignum-1.0/GHC-Num-BigNat.html#v:bigNatLogBase-35- | ||
-/ | ||
@[pp_nodot] def logC (b m : ℕ) : ℕ := | ||
if h : 1 < b then let (_, e) := step b h; e else 0 where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional (I didn't test if it results in the same IR):
if h : 1 < b then let (_, e) := step b h; e else 0 where | |
if h : 1 < b then (step b h).2 else 0 where |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bors d+
#eval Nat.logTR 2 (2 ^ 1000000) | ||
#eval Nat.logC 2 (2 ^ 1000000) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean in particular that these aren't meant as regression tests?
✌️ b-mehta can now approve this pull request. To approve and merge a pull request, simply reply with |
bors merge |
Co-authored-by: Eric Wieser <[email protected]>
Canceled. |
Can you also update the PR description with the changes and their reason (for the people who grep the git log)? Thanks. |
Updated. |
Give an alternate definition
Nat.logC
forNat.log
which computes more efficiently (roughly speaking, this should have logarithmic time in the output, whileNat.log
has linear time in its output).Prove that these two give the same output, and use
csimp
to ensure evaluation ofNat.log
usesNat.logC
instead.